hostapd_cli.c 40 KB

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