hostapd_cli.c 41 KB

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