hostapd_cli.c 42 KB

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