hostapd_cli.c 42 KB

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