hostapd_cli.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * hostapd - command line interface for hostapd daemon
  3. * Copyright (c) 2004-2017, 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-2017, 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[2048];
  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 char ** hostapd_complete_set(const char *str, int pos)
  853. {
  854. int arg = get_cmd_arg_num(str, pos);
  855. const char *fields[] = {
  856. #ifdef CONFIG_WPS_TESTING
  857. "wps_version_number", "wps_testing_dummy_cred",
  858. "wps_corrupt_pkhash",
  859. #endif /* CONFIG_WPS_TESTING */
  860. #ifdef CONFIG_INTERWORKING
  861. "gas_frag_limit",
  862. #endif /* CONFIG_INTERWORKING */
  863. #ifdef CONFIG_TESTING_OPTIONS
  864. "ext_mgmt_frame_handling", "ext_eapol_frame_io",
  865. #endif /* CONFIG_TESTING_OPTIONS */
  866. #ifdef CONFIG_MBO
  867. "mbo_assoc_disallow",
  868. #endif /* CONFIG_MBO */
  869. "deny_mac_file", "accept_mac_file",
  870. };
  871. int i, num_fields = ARRAY_SIZE(fields);
  872. if (arg == 1) {
  873. char **res;
  874. res = os_calloc(num_fields + 1, sizeof(char *));
  875. if (!res)
  876. return NULL;
  877. for (i = 0; i < num_fields; i++) {
  878. res[i] = os_strdup(fields[i]);
  879. if (!res[i])
  880. return res;
  881. }
  882. return res;
  883. }
  884. return NULL;
  885. }
  886. static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  887. {
  888. char cmd[256];
  889. int res;
  890. if (argc != 1) {
  891. printf("Invalid GET command: needs one argument (variable "
  892. "name)\n");
  893. return -1;
  894. }
  895. res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
  896. if (os_snprintf_error(sizeof(cmd), res)) {
  897. printf("Too long GET command.\n");
  898. return -1;
  899. }
  900. return wpa_ctrl_command(ctrl, cmd);
  901. }
  902. static char ** hostapd_complete_get(const char *str, int pos)
  903. {
  904. int arg = get_cmd_arg_num(str, pos);
  905. const char *fields[] = {
  906. "version", "tls_library",
  907. };
  908. int i, num_fields = ARRAY_SIZE(fields);
  909. if (arg == 1) {
  910. char **res;
  911. res = os_calloc(num_fields + 1, sizeof(char *));
  912. if (!res)
  913. return NULL;
  914. for (i = 0; i < num_fields; i++) {
  915. res[i] = os_strdup(fields[i]);
  916. if (!res[i])
  917. return res;
  918. }
  919. return res;
  920. }
  921. return NULL;
  922. }
  923. #ifdef CONFIG_FST
  924. static int hostapd_cli_cmd_fst(struct wpa_ctrl *ctrl, int argc, char *argv[])
  925. {
  926. char cmd[256];
  927. int res;
  928. int i;
  929. int total;
  930. if (argc <= 0) {
  931. printf("FST command: parameters are required.\n");
  932. return -1;
  933. }
  934. total = os_snprintf(cmd, sizeof(cmd), "FST-MANAGER");
  935. for (i = 0; i < argc; i++) {
  936. res = os_snprintf(cmd + total, sizeof(cmd) - total, " %s",
  937. argv[i]);
  938. if (os_snprintf_error(sizeof(cmd) - total, res)) {
  939. printf("Too long fst command.\n");
  940. return -1;
  941. }
  942. total += res;
  943. }
  944. return wpa_ctrl_command(ctrl, cmd);
  945. }
  946. #endif /* CONFIG_FST */
  947. static int hostapd_cli_cmd_chan_switch(struct wpa_ctrl *ctrl,
  948. int argc, char *argv[])
  949. {
  950. char cmd[256];
  951. int res;
  952. int i;
  953. char *tmp;
  954. int total;
  955. if (argc < 2) {
  956. printf("Invalid chan_switch command: needs at least two "
  957. "arguments (count and freq)\n"
  958. "usage: <cs_count> <freq> [sec_channel_offset=] "
  959. "[center_freq1=] [center_freq2=] [bandwidth=] "
  960. "[blocktx] [ht|vht]\n");
  961. return -1;
  962. }
  963. res = os_snprintf(cmd, sizeof(cmd), "CHAN_SWITCH %s %s",
  964. argv[0], argv[1]);
  965. if (os_snprintf_error(sizeof(cmd), res)) {
  966. printf("Too long CHAN_SWITCH command.\n");
  967. return -1;
  968. }
  969. total = res;
  970. for (i = 2; i < argc; i++) {
  971. tmp = cmd + total;
  972. res = os_snprintf(tmp, sizeof(cmd) - total, " %s", argv[i]);
  973. if (os_snprintf_error(sizeof(cmd) - total, res)) {
  974. printf("Too long CHAN_SWITCH command.\n");
  975. return -1;
  976. }
  977. total += res;
  978. }
  979. return wpa_ctrl_command(ctrl, cmd);
  980. }
  981. static int hostapd_cli_cmd_enable(struct wpa_ctrl *ctrl, int argc,
  982. char *argv[])
  983. {
  984. return wpa_ctrl_command(ctrl, "ENABLE");
  985. }
  986. static int hostapd_cli_cmd_reload(struct wpa_ctrl *ctrl, int argc,
  987. char *argv[])
  988. {
  989. return wpa_ctrl_command(ctrl, "RELOAD");
  990. }
  991. static int hostapd_cli_cmd_disable(struct wpa_ctrl *ctrl, int argc,
  992. char *argv[])
  993. {
  994. return wpa_ctrl_command(ctrl, "DISABLE");
  995. }
  996. static int hostapd_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
  997. {
  998. char cmd[256];
  999. int res;
  1000. if (argc < 2 || argc > 3) {
  1001. printf("Invalid vendor command\n"
  1002. "usage: <vendor id> <command id> [<hex formatted command argument>]\n");
  1003. return -1;
  1004. }
  1005. res = os_snprintf(cmd, sizeof(cmd), "VENDOR %s %s %s", argv[0], argv[1],
  1006. argc == 3 ? argv[2] : "");
  1007. if (os_snprintf_error(sizeof(cmd), res)) {
  1008. printf("Too long VENDOR command.\n");
  1009. return -1;
  1010. }
  1011. return wpa_ctrl_command(ctrl, cmd);
  1012. }
  1013. static int hostapd_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc,
  1014. char *argv[])
  1015. {
  1016. return wpa_ctrl_command(ctrl, "ERP_FLUSH");
  1017. }
  1018. static int hostapd_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc,
  1019. char *argv[])
  1020. {
  1021. char cmd[256];
  1022. int res;
  1023. res = os_snprintf(cmd, sizeof(cmd), "LOG_LEVEL%s%s%s%s",
  1024. argc >= 1 ? " " : "",
  1025. argc >= 1 ? argv[0] : "",
  1026. argc == 2 ? " " : "",
  1027. argc == 2 ? argv[1] : "");
  1028. if (os_snprintf_error(sizeof(cmd), res)) {
  1029. printf("Too long option\n");
  1030. return -1;
  1031. }
  1032. return wpa_ctrl_command(ctrl, cmd);
  1033. }
  1034. static int hostapd_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1035. {
  1036. if (argc == 0)
  1037. return -1;
  1038. return hostapd_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
  1039. }
  1040. static int hostapd_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1041. {
  1042. return wpa_ctrl_command(ctrl, "PMKSA");
  1043. }
  1044. static int hostapd_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
  1045. char *argv[])
  1046. {
  1047. return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
  1048. }
  1049. static int hostapd_cli_cmd_set_neighbor(struct wpa_ctrl *ctrl, int argc,
  1050. char *argv[])
  1051. {
  1052. char cmd[2048];
  1053. int res;
  1054. if (argc < 3 || argc > 6) {
  1055. printf("Invalid set_neighbor command: needs 3-6 arguments\n");
  1056. return -1;
  1057. }
  1058. res = os_snprintf(cmd, sizeof(cmd), "SET_NEIGHBOR %s %s %s %s %s %s",
  1059. argv[0], argv[1], argv[2], argc >= 4 ? argv[3] : "",
  1060. argc >= 5 ? argv[4] : "", argc == 6 ? argv[5] : "");
  1061. if (os_snprintf_error(sizeof(cmd), res)) {
  1062. printf("Too long SET_NEIGHBOR command.\n");
  1063. return -1;
  1064. }
  1065. return wpa_ctrl_command(ctrl, cmd);
  1066. }
  1067. static int hostapd_cli_cmd_remove_neighbor(struct wpa_ctrl *ctrl, int argc,
  1068. char *argv[])
  1069. {
  1070. char cmd[400];
  1071. int res;
  1072. if (argc != 2) {
  1073. printf("Invalid remove_neighbor command: needs 2 arguments\n");
  1074. return -1;
  1075. }
  1076. res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NEIGHBOR %s %s",
  1077. argv[0], argv[1]);
  1078. if (os_snprintf_error(sizeof(cmd), res)) {
  1079. printf("Too long REMOVE_NEIGHBOR command.\n");
  1080. return -1;
  1081. }
  1082. return wpa_ctrl_command(ctrl, cmd);
  1083. }
  1084. static int hostapd_cli_cmd_req_lci(struct wpa_ctrl *ctrl, int argc,
  1085. char *argv[])
  1086. {
  1087. char cmd[256];
  1088. int res;
  1089. if (argc != 1) {
  1090. printf("Invalid req_lci command - requires destination address\n");
  1091. return -1;
  1092. }
  1093. res = os_snprintf(cmd, sizeof(cmd), "REQ_LCI %s", argv[0]);
  1094. if (os_snprintf_error(sizeof(cmd), res)) {
  1095. printf("Too long REQ_LCI command.\n");
  1096. return -1;
  1097. }
  1098. return wpa_ctrl_command(ctrl, cmd);
  1099. }
  1100. static int hostapd_cli_cmd_req_range(struct wpa_ctrl *ctrl, int argc,
  1101. char *argv[])
  1102. {
  1103. if (argc < 4) {
  1104. printf("Invalid req_range command: needs at least 4 arguments - dest address, randomization interval, min AP count, and 1 to 16 AP addresses\n");
  1105. return -1;
  1106. }
  1107. return hostapd_cli_cmd(ctrl, "REQ_RANGE", 4, argc, argv);
  1108. }
  1109. static int hostapd_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
  1110. char *argv[])
  1111. {
  1112. return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
  1113. }
  1114. #ifdef CONFIG_DPP
  1115. static int hostapd_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
  1116. char *argv[])
  1117. {
  1118. return hostapd_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
  1119. }
  1120. static int hostapd_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
  1121. char *argv[])
  1122. {
  1123. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
  1124. }
  1125. static int hostapd_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
  1126. char *argv[])
  1127. {
  1128. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
  1129. }
  1130. static int hostapd_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl,
  1131. int argc, char *argv[])
  1132. {
  1133. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
  1134. }
  1135. static int hostapd_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
  1136. char *argv[])
  1137. {
  1138. return hostapd_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
  1139. }
  1140. static int hostapd_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
  1141. char *argv[])
  1142. {
  1143. return hostapd_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
  1144. }
  1145. static int hostapd_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
  1146. char *argv[])
  1147. {
  1148. return hostapd_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
  1149. }
  1150. static int hostapd_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
  1151. char *argv[])
  1152. {
  1153. return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
  1154. }
  1155. static int hostapd_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
  1156. char *argv[])
  1157. {
  1158. return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
  1159. }
  1160. static int hostapd_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl,
  1161. int argc, char *argv[])
  1162. {
  1163. return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
  1164. }
  1165. static int hostapd_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl,
  1166. int argc, char *argv[])
  1167. {
  1168. return hostapd_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
  1169. }
  1170. static int hostapd_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
  1171. char *argv[])
  1172. {
  1173. return hostapd_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
  1174. }
  1175. static int hostapd_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
  1176. char *argv[])
  1177. {
  1178. return hostapd_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
  1179. }
  1180. #endif /* CONFIG_DPP */
  1181. static int hostapd_cli_cmd_accept_macacl(struct wpa_ctrl *ctrl, int argc,
  1182. char *argv[])
  1183. {
  1184. return hostapd_cli_cmd(ctrl, "ACCEPT_ACL", 1, argc, argv);
  1185. }
  1186. static int hostapd_cli_cmd_deny_macacl(struct wpa_ctrl *ctrl, int argc,
  1187. char *argv[])
  1188. {
  1189. return hostapd_cli_cmd(ctrl, "DENY_ACL", 1, argc, argv);
  1190. }
  1191. static int hostapd_cli_cmd_poll_sta(struct wpa_ctrl *ctrl, int argc,
  1192. char *argv[])
  1193. {
  1194. return hostapd_cli_cmd(ctrl, "POLL_STA", 1, argc, argv);
  1195. }
  1196. struct hostapd_cli_cmd {
  1197. const char *cmd;
  1198. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  1199. char ** (*completion)(const char *str, int pos);
  1200. const char *usage;
  1201. };
  1202. static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
  1203. { "ping", hostapd_cli_cmd_ping, NULL,
  1204. "= pings hostapd" },
  1205. { "mib", hostapd_cli_cmd_mib, NULL,
  1206. "= get MIB variables (dot1x, dot11, radius)" },
  1207. { "relog", hostapd_cli_cmd_relog, NULL,
  1208. "= reload/truncate debug log output file" },
  1209. { "status", hostapd_cli_cmd_status, NULL,
  1210. "= show interface status info" },
  1211. { "sta", hostapd_cli_cmd_sta, hostapd_complete_stations,
  1212. "<addr> = get MIB variables for one station" },
  1213. { "all_sta", hostapd_cli_cmd_all_sta, NULL,
  1214. "= get MIB variables for all stations" },
  1215. { "list_sta", hostapd_cli_cmd_list_sta, NULL,
  1216. "= list all stations" },
  1217. { "new_sta", hostapd_cli_cmd_new_sta, NULL,
  1218. "<addr> = add a new station" },
  1219. { "deauthenticate", hostapd_cli_cmd_deauthenticate,
  1220. hostapd_complete_stations,
  1221. "<addr> = deauthenticate a station" },
  1222. { "disassociate", hostapd_cli_cmd_disassociate,
  1223. hostapd_complete_stations,
  1224. "<addr> = disassociate a station" },
  1225. #ifdef CONFIG_TAXONOMY
  1226. { "signature", hostapd_cli_cmd_signature, hostapd_complete_stations,
  1227. "<addr> = get taxonomy signature for a station" },
  1228. #endif /* CONFIG_TAXONOMY */
  1229. #ifdef CONFIG_IEEE80211W
  1230. { "sa_query", hostapd_cli_cmd_sa_query, hostapd_complete_stations,
  1231. "<addr> = send SA Query to a station" },
  1232. #endif /* CONFIG_IEEE80211W */
  1233. #ifdef CONFIG_WPS
  1234. { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
  1235. "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
  1236. { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
  1237. "<PIN> = verify PIN checksum" },
  1238. { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
  1239. "= indicate button pushed to initiate PBC" },
  1240. { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
  1241. "= cancel the pending WPS operation" },
  1242. #ifdef CONFIG_WPS_NFC
  1243. { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
  1244. "<hexdump> = report read NFC tag with WPS data" },
  1245. { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
  1246. "<WPS/NDEF> = build NFC configuration token" },
  1247. { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
  1248. "<WPS/NDEF/enable/disable> = manager NFC password token" },
  1249. { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
  1250. NULL },
  1251. #endif /* CONFIG_WPS_NFC */
  1252. { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
  1253. "<cmd> [params..] = enable/disable AP PIN" },
  1254. { "wps_config", hostapd_cli_cmd_wps_config, NULL,
  1255. "<SSID> <auth> <encr> <key> = configure AP" },
  1256. { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
  1257. "= show current WPS status" },
  1258. #endif /* CONFIG_WPS */
  1259. { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL,
  1260. "= send Disassociation Imminent notification" },
  1261. { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL,
  1262. "= send ESS Dissassociation Imminent notification" },
  1263. { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL,
  1264. "= send BSS Transition Management Request" },
  1265. { "get_config", hostapd_cli_cmd_get_config, NULL,
  1266. "= show current configuration" },
  1267. { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
  1268. "= show this usage help" },
  1269. { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
  1270. "[ifname] = show interfaces/select interface" },
  1271. #ifdef CONFIG_FST
  1272. { "fst", hostapd_cli_cmd_fst, NULL,
  1273. "<params...> = send FST-MANAGER control interface command" },
  1274. #endif /* CONFIG_FST */
  1275. { "raw", hostapd_cli_cmd_raw, NULL,
  1276. "<params..> = send unprocessed command" },
  1277. { "level", hostapd_cli_cmd_level, NULL,
  1278. "<debug level> = change debug level" },
  1279. { "license", hostapd_cli_cmd_license, NULL,
  1280. "= show full hostapd_cli license" },
  1281. { "quit", hostapd_cli_cmd_quit, NULL,
  1282. "= exit hostapd_cli" },
  1283. { "set", hostapd_cli_cmd_set, hostapd_complete_set,
  1284. "<name> <value> = set runtime variables" },
  1285. { "get", hostapd_cli_cmd_get, hostapd_complete_get,
  1286. "<name> = get runtime info" },
  1287. { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL,
  1288. "<arg,arg,...> = set QoS Map set element" },
  1289. { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf,
  1290. hostapd_complete_stations,
  1291. "<addr> = send QoS Map Configure frame" },
  1292. { "chan_switch", hostapd_cli_cmd_chan_switch, NULL,
  1293. "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]\n"
  1294. " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]\n"
  1295. " = initiate channel switch announcement" },
  1296. { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL,
  1297. "<addr> <url>\n"
  1298. " = send WNM-Notification Subscription Remediation Request" },
  1299. { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL,
  1300. "<addr> <code (0/1)> <Re-auth-Delay(sec)> [url]\n"
  1301. " = send WNM-Notification imminent deauthentication indication" },
  1302. { "vendor", hostapd_cli_cmd_vendor, NULL,
  1303. "<vendor id> <sub command id> [<hex formatted data>]\n"
  1304. " = send vendor driver command" },
  1305. { "enable", hostapd_cli_cmd_enable, NULL,
  1306. "= enable hostapd on current interface" },
  1307. { "reload", hostapd_cli_cmd_reload, NULL,
  1308. "= reload configuration for current interface" },
  1309. { "disable", hostapd_cli_cmd_disable, NULL,
  1310. "= disable hostapd on current interface" },
  1311. { "erp_flush", hostapd_cli_cmd_erp_flush, NULL,
  1312. "= drop all ERP keys"},
  1313. { "log_level", hostapd_cli_cmd_log_level, NULL,
  1314. "[level] = show/change log verbosity level" },
  1315. { "pmksa", hostapd_cli_cmd_pmksa, NULL,
  1316. " = show PMKSA cache entries" },
  1317. { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL,
  1318. " = flush PMKSA cache" },
  1319. { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL,
  1320. "<addr> <ssid=> <nr=> [lci=] [civic=] [stat]\n"
  1321. " = add AP to neighbor database" },
  1322. { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL,
  1323. "<addr> <ssid=> = remove AP from neighbor database" },
  1324. { "req_lci", hostapd_cli_cmd_req_lci, hostapd_complete_stations,
  1325. "<addr> = send LCI request to a station"},
  1326. { "req_range", hostapd_cli_cmd_req_range, NULL,
  1327. " = send FTM range request"},
  1328. { "driver_flags", hostapd_cli_cmd_driver_flags, NULL,
  1329. " = show supported driver flags"},
  1330. #ifdef CONFIG_DPP
  1331. { "dpp_qr_code", hostapd_cli_cmd_dpp_qr_code, NULL,
  1332. "report a scanned DPP URI from a QR Code" },
  1333. { "dpp_bootstrap_gen", hostapd_cli_cmd_dpp_bootstrap_gen, NULL,
  1334. "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
  1335. { "dpp_bootstrap_remove", hostapd_cli_cmd_dpp_bootstrap_remove, NULL,
  1336. "*|<id> = remove DPP bootstrap information" },
  1337. { "dpp_bootstrap_get_uri", hostapd_cli_cmd_dpp_bootstrap_get_uri, NULL,
  1338. "<id> = get DPP bootstrap URI" },
  1339. { "dpp_bootstrap_info", hostapd_cli_cmd_dpp_bootstrap_info, NULL,
  1340. "<id> = show DPP bootstrap information" },
  1341. { "dpp_auth_init", hostapd_cli_cmd_dpp_auth_init, NULL,
  1342. "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
  1343. { "dpp_listen", hostapd_cli_cmd_dpp_listen, NULL,
  1344. "<freq in MHz> = start DPP listen" },
  1345. { "dpp_stop_listen", hostapd_cli_cmd_dpp_stop_listen, NULL,
  1346. "= stop DPP listen" },
  1347. { "dpp_configurator_add", hostapd_cli_cmd_dpp_configurator_add, NULL,
  1348. "[curve=..] [key=..] = add DPP configurator" },
  1349. { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_remove,
  1350. NULL,
  1351. "*|<id> = remove DPP configurator" },
  1352. { "dpp_configurator_remove", hostapd_cli_cmd_dpp_configurator_get_key,
  1353. NULL,
  1354. "<id> = Get DPP configurator's private key" },
  1355. { "dpp_pkex_add", hostapd_cli_cmd_dpp_pkex_add, NULL,
  1356. "add PKEX code" },
  1357. { "dpp_pkex_remove", hostapd_cli_cmd_dpp_pkex_remove, NULL,
  1358. "*|<id> = remove DPP pkex information" },
  1359. #endif /* CONFIG_DPP */
  1360. { "accept_acl", hostapd_cli_cmd_accept_macacl, NULL,
  1361. "=Add/Delete/Show/Clear accept MAC ACL" },
  1362. { "deny_acl", hostapd_cli_cmd_deny_macacl, NULL,
  1363. "=Add/Delete/Show/Clear deny MAC ACL" },
  1364. { "poll_sta", hostapd_cli_cmd_poll_sta, hostapd_complete_stations,
  1365. "<addr> = poll a STA to check connectivity with a QoS null frame" },
  1366. { NULL, NULL, NULL, NULL }
  1367. };
  1368. /*
  1369. * Prints command usage, lines are padded with the specified string.
  1370. */
  1371. static void print_cmd_help(FILE *stream, const struct hostapd_cli_cmd *cmd,
  1372. const char *pad)
  1373. {
  1374. char c;
  1375. size_t n;
  1376. if (cmd->usage == NULL)
  1377. return;
  1378. fprintf(stream, "%s%s ", pad, cmd->cmd);
  1379. for (n = 0; (c = cmd->usage[n]); n++) {
  1380. fprintf(stream, "%c", c);
  1381. if (c == '\n')
  1382. fprintf(stream, "%s", pad);
  1383. }
  1384. fprintf(stream, "\n");
  1385. }
  1386. static void print_help(FILE *stream, const char *cmd)
  1387. {
  1388. int n;
  1389. fprintf(stream, "commands:\n");
  1390. for (n = 0; hostapd_cli_commands[n].cmd; n++) {
  1391. if (cmd == NULL || str_starts(hostapd_cli_commands[n].cmd, cmd))
  1392. print_cmd_help(stream, &hostapd_cli_commands[n], " ");
  1393. }
  1394. }
  1395. static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1396. {
  1397. const struct hostapd_cli_cmd *cmd, *match = NULL;
  1398. int count;
  1399. count = 0;
  1400. cmd = hostapd_cli_commands;
  1401. while (cmd->cmd) {
  1402. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
  1403. match = cmd;
  1404. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  1405. /* we have an exact match */
  1406. count = 1;
  1407. break;
  1408. }
  1409. count++;
  1410. }
  1411. cmd++;
  1412. }
  1413. if (count > 1) {
  1414. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  1415. cmd = hostapd_cli_commands;
  1416. while (cmd->cmd) {
  1417. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
  1418. 0) {
  1419. printf(" %s", cmd->cmd);
  1420. }
  1421. cmd++;
  1422. }
  1423. printf("\n");
  1424. } else if (count == 0) {
  1425. printf("Unknown command '%s'\n", argv[0]);
  1426. } else {
  1427. match->handler(ctrl, argc - 1, &argv[1]);
  1428. }
  1429. }
  1430. static void cli_event(const char *str)
  1431. {
  1432. const char *start, *s;
  1433. start = os_strchr(str, '>');
  1434. if (start == NULL)
  1435. return;
  1436. start++;
  1437. if (str_starts(start, AP_STA_CONNECTED)) {
  1438. s = os_strchr(start, ' ');
  1439. if (s == NULL)
  1440. return;
  1441. cli_txt_list_add(&stations, s + 1);
  1442. return;
  1443. }
  1444. if (str_starts(start, AP_STA_DISCONNECTED)) {
  1445. s = os_strchr(start, ' ');
  1446. if (s == NULL)
  1447. return;
  1448. cli_txt_list_del_addr(&stations, s + 1);
  1449. return;
  1450. }
  1451. }
  1452. static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
  1453. int action_monitor)
  1454. {
  1455. int first = 1;
  1456. if (ctrl_conn == NULL)
  1457. return;
  1458. while (wpa_ctrl_pending(ctrl)) {
  1459. char buf[4096];
  1460. size_t len = sizeof(buf) - 1;
  1461. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  1462. buf[len] = '\0';
  1463. if (action_monitor)
  1464. hostapd_cli_action_process(buf, len);
  1465. else {
  1466. cli_event(buf);
  1467. if (in_read && first)
  1468. printf("\n");
  1469. first = 0;
  1470. printf("%s\n", buf);
  1471. }
  1472. } else {
  1473. printf("Could not read pending message.\n");
  1474. break;
  1475. }
  1476. }
  1477. }
  1478. static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
  1479. {
  1480. hostapd_cli_recv_pending(ctrl_conn, 0, 0);
  1481. }
  1482. static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
  1483. {
  1484. if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
  1485. printf("Connection to hostapd lost - trying to reconnect\n");
  1486. hostapd_cli_close_connection();
  1487. }
  1488. if (!ctrl_conn && hostapd_cli_reconnect(ctrl_ifname) == 0)
  1489. printf("Connection to hostapd re-established\n");
  1490. if (ctrl_conn)
  1491. hostapd_cli_recv_pending(ctrl_conn, 1, 0);
  1492. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1493. }
  1494. static void hostapd_cli_eloop_terminate(int sig, void *signal_ctx)
  1495. {
  1496. eloop_terminate();
  1497. }
  1498. static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd)
  1499. {
  1500. char *argv[max_args];
  1501. int argc;
  1502. argc = tokenize_cmd(cmd, argv);
  1503. if (argc)
  1504. wpa_request(ctrl_conn, argc, argv);
  1505. }
  1506. static void hostapd_cli_edit_eof_cb(void *ctx)
  1507. {
  1508. eloop_terminate();
  1509. }
  1510. static char ** list_cmd_list(void)
  1511. {
  1512. char **res;
  1513. int i, count;
  1514. count = ARRAY_SIZE(hostapd_cli_commands);
  1515. res = os_calloc(count + 1, sizeof(char *));
  1516. if (res == NULL)
  1517. return NULL;
  1518. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1519. res[i] = os_strdup(hostapd_cli_commands[i].cmd);
  1520. if (res[i] == NULL)
  1521. break;
  1522. }
  1523. return res;
  1524. }
  1525. static char ** hostapd_cli_cmd_completion(const char *cmd, const char *str,
  1526. int pos)
  1527. {
  1528. int i;
  1529. for (i = 0; hostapd_cli_commands[i].cmd; i++) {
  1530. if (os_strcasecmp(hostapd_cli_commands[i].cmd, cmd) != 0)
  1531. continue;
  1532. if (hostapd_cli_commands[i].completion)
  1533. return hostapd_cli_commands[i].completion(str, pos);
  1534. if (!hostapd_cli_commands[i].usage)
  1535. return NULL;
  1536. edit_clear_line();
  1537. printf("\r%s\n", hostapd_cli_commands[i].usage);
  1538. edit_redraw();
  1539. break;
  1540. }
  1541. return NULL;
  1542. }
  1543. static char ** hostapd_cli_edit_completion_cb(void *ctx, const char *str,
  1544. int pos)
  1545. {
  1546. char **res;
  1547. const char *end;
  1548. char *cmd;
  1549. end = os_strchr(str, ' ');
  1550. if (end == NULL || str + pos < end)
  1551. return list_cmd_list();
  1552. cmd = os_malloc(pos + 1);
  1553. if (cmd == NULL)
  1554. return NULL;
  1555. os_memcpy(cmd, str, pos);
  1556. cmd[end - str] = '\0';
  1557. res = hostapd_cli_cmd_completion(cmd, str, pos);
  1558. os_free(cmd);
  1559. return res;
  1560. }
  1561. static void hostapd_cli_interactive(void)
  1562. {
  1563. char *hfile = NULL;
  1564. char *home;
  1565. printf("\nInteractive mode\n\n");
  1566. #ifdef CONFIG_HOSTAPD_CLI_HISTORY_DIR
  1567. home = CONFIG_HOSTAPD_CLI_HISTORY_DIR;
  1568. #else /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1569. home = getenv("HOME");
  1570. #endif /* CONFIG_HOSTAPD_CLI_HISTORY_DIR */
  1571. if (home) {
  1572. const char *fname = ".hostapd_cli_history";
  1573. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  1574. hfile = os_malloc(hfile_len);
  1575. if (hfile)
  1576. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  1577. }
  1578. eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
  1579. edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
  1580. hostapd_cli_edit_completion_cb, NULL, hfile, NULL);
  1581. eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
  1582. eloop_run();
  1583. cli_txt_list_flush(&stations);
  1584. edit_deinit(hfile, NULL);
  1585. os_free(hfile);
  1586. eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
  1587. }
  1588. static void hostapd_cli_cleanup(void)
  1589. {
  1590. hostapd_cli_close_connection();
  1591. if (pid_file)
  1592. os_daemonize_terminate(pid_file);
  1593. os_program_deinit();
  1594. }
  1595. static void hostapd_cli_action(struct wpa_ctrl *ctrl)
  1596. {
  1597. fd_set rfds;
  1598. int fd, res;
  1599. struct timeval tv;
  1600. char buf[256];
  1601. size_t len;
  1602. fd = wpa_ctrl_get_fd(ctrl);
  1603. while (!hostapd_cli_quit) {
  1604. FD_ZERO(&rfds);
  1605. FD_SET(fd, &rfds);
  1606. tv.tv_sec = ping_interval;
  1607. tv.tv_usec = 0;
  1608. res = select(fd + 1, &rfds, NULL, NULL, &tv);
  1609. if (res < 0 && errno != EINTR) {
  1610. perror("select");
  1611. break;
  1612. }
  1613. if (FD_ISSET(fd, &rfds))
  1614. hostapd_cli_recv_pending(ctrl, 0, 1);
  1615. else {
  1616. len = sizeof(buf) - 1;
  1617. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  1618. hostapd_cli_action_process) < 0 ||
  1619. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  1620. printf("hostapd did not reply to PING "
  1621. "command - exiting\n");
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. int main(int argc, char *argv[])
  1628. {
  1629. int warning_displayed = 0;
  1630. int c;
  1631. int daemonize = 0;
  1632. if (os_program_init())
  1633. return -1;
  1634. for (;;) {
  1635. c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
  1636. if (c < 0)
  1637. break;
  1638. switch (c) {
  1639. case 'a':
  1640. action_file = optarg;
  1641. break;
  1642. case 'B':
  1643. daemonize = 1;
  1644. break;
  1645. case 'G':
  1646. ping_interval = atoi(optarg);
  1647. break;
  1648. case 'h':
  1649. usage();
  1650. return 0;
  1651. case 'v':
  1652. printf("%s\n", hostapd_cli_version);
  1653. return 0;
  1654. case 'i':
  1655. os_free(ctrl_ifname);
  1656. ctrl_ifname = os_strdup(optarg);
  1657. break;
  1658. case 'p':
  1659. ctrl_iface_dir = optarg;
  1660. break;
  1661. case 'P':
  1662. pid_file = optarg;
  1663. break;
  1664. case 's':
  1665. client_socket_dir = optarg;
  1666. break;
  1667. default:
  1668. usage();
  1669. return -1;
  1670. }
  1671. }
  1672. interactive = (argc == optind) && (action_file == NULL);
  1673. if (interactive) {
  1674. printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
  1675. }
  1676. if (eloop_init())
  1677. return -1;
  1678. for (;;) {
  1679. if (ctrl_ifname == NULL) {
  1680. struct dirent *dent;
  1681. DIR *dir = opendir(ctrl_iface_dir);
  1682. if (dir) {
  1683. while ((dent = readdir(dir))) {
  1684. if (os_strcmp(dent->d_name, ".") == 0
  1685. ||
  1686. os_strcmp(dent->d_name, "..") == 0)
  1687. continue;
  1688. printf("Selected interface '%s'\n",
  1689. dent->d_name);
  1690. ctrl_ifname = os_strdup(dent->d_name);
  1691. break;
  1692. }
  1693. closedir(dir);
  1694. }
  1695. }
  1696. hostapd_cli_reconnect(ctrl_ifname);
  1697. if (ctrl_conn) {
  1698. if (warning_displayed)
  1699. printf("Connection established.\n");
  1700. break;
  1701. }
  1702. if (!interactive) {
  1703. perror("Failed to connect to hostapd - "
  1704. "wpa_ctrl_open");
  1705. return -1;
  1706. }
  1707. if (!warning_displayed) {
  1708. printf("Could not connect to hostapd - re-trying\n");
  1709. warning_displayed = 1;
  1710. }
  1711. os_sleep(1, 0);
  1712. continue;
  1713. }
  1714. if (action_file && !hostapd_cli_attached)
  1715. return -1;
  1716. if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
  1717. return -1;
  1718. if (interactive)
  1719. hostapd_cli_interactive();
  1720. else if (action_file)
  1721. hostapd_cli_action(ctrl_conn);
  1722. else
  1723. wpa_request(ctrl_conn, argc - optind, &argv[optind]);
  1724. unregister_event_handler(ctrl_conn);
  1725. os_free(ctrl_ifname);
  1726. eloop_destroy();
  1727. hostapd_cli_cleanup();
  1728. return 0;
  1729. }
  1730. #else /* CONFIG_NO_CTRL_IFACE */
  1731. int main(int argc, char *argv[])
  1732. {
  1733. return -1;
  1734. }
  1735. #endif /* CONFIG_NO_CTRL_IFACE */