選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hostapd_cli.c 25KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * hostapd - command line interface for hostapd daemon
  3. * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <dirent.h>
  16. #include "common/wpa_ctrl.h"
  17. #include "common.h"
  18. #include "common/version.h"
  19. static const char *hostapd_cli_version =
  20. "hostapd_cli v" VERSION_STR "\n"
  21. "Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi> and contributors";
  22. static const char *hostapd_cli_license =
  23. "This program is free software. You can distribute it and/or modify it\n"
  24. "under the terms of the GNU General Public License version 2.\n"
  25. "\n"
  26. "Alternatively, this software may be distributed under the terms of the\n"
  27. "BSD license. See README and COPYING for more details.\n";
  28. static const char *hostapd_cli_full_license =
  29. "This program is free software; you can redistribute it and/or modify\n"
  30. "it under the terms of the GNU General Public License version 2 as\n"
  31. "published by the Free Software Foundation.\n"
  32. "\n"
  33. "This program is distributed in the hope that it will be useful,\n"
  34. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  35. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  36. "GNU General Public License for more details.\n"
  37. "\n"
  38. "You should have received a copy of the GNU General Public License\n"
  39. "along with this program; if not, write to the Free Software\n"
  40. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  41. "\n"
  42. "Alternatively, this software may be distributed under the terms of the\n"
  43. "BSD license.\n"
  44. "\n"
  45. "Redistribution and use in source and binary forms, with or without\n"
  46. "modification, are permitted provided that the following conditions are\n"
  47. "met:\n"
  48. "\n"
  49. "1. Redistributions of source code must retain the above copyright\n"
  50. " notice, this list of conditions and the following disclaimer.\n"
  51. "\n"
  52. "2. Redistributions in binary form must reproduce the above copyright\n"
  53. " notice, this list of conditions and the following disclaimer in the\n"
  54. " documentation and/or other materials provided with the distribution.\n"
  55. "\n"
  56. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  57. " names of its contributors may be used to endorse or promote products\n"
  58. " derived from this software without specific prior written permission.\n"
  59. "\n"
  60. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  61. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  62. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  63. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
  64. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  65. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  66. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  67. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  68. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  69. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  70. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  71. "\n";
  72. static const char *commands_help =
  73. "Commands:\n"
  74. " mib get MIB variables (dot1x, dot11, radius)\n"
  75. " sta <addr> get MIB variables for one station\n"
  76. " all_sta get MIB variables for all stations\n"
  77. " new_sta <addr> add a new station\n"
  78. " deauthenticate <addr> deauthenticate a station\n"
  79. " disassociate <addr> disassociate a station\n"
  80. #ifdef CONFIG_IEEE80211W
  81. " sa_query <addr> send SA Query to a station\n"
  82. #endif /* CONFIG_IEEE80211W */
  83. #ifdef CONFIG_WPS
  84. " wps_pin <uuid> <pin> [timeout] [addr] add WPS Enrollee PIN\n"
  85. " wps_check_pin <PIN> verify PIN checksum\n"
  86. " wps_pbc indicate button pushed to initiate PBC\n"
  87. #ifdef CONFIG_WPS_OOB
  88. " wps_oob <type> <path> <method> use WPS with out-of-band (UFD)\n"
  89. #endif /* CONFIG_WPS_OOB */
  90. " wps_ap_pin <cmd> [params..] enable/disable AP PIN\n"
  91. " wps_config <SSID> <auth> <encr> <key> configure AP\n"
  92. #endif /* CONFIG_WPS */
  93. " get_config show current configuration\n"
  94. " help show this usage help\n"
  95. " interface [ifname] show interfaces/select interface\n"
  96. " level <debug level> change debug level\n"
  97. " license show full hostapd_cli license\n"
  98. " quit exit hostapd_cli\n";
  99. static struct wpa_ctrl *ctrl_conn;
  100. static int hostapd_cli_quit = 0;
  101. static int hostapd_cli_attached = 0;
  102. static const char *ctrl_iface_dir = "/var/run/hostapd";
  103. static char *ctrl_ifname = NULL;
  104. static const char *pid_file = NULL;
  105. static const char *action_file = NULL;
  106. static int ping_interval = 5;
  107. static void usage(void)
  108. {
  109. fprintf(stderr, "%s\n", hostapd_cli_version);
  110. fprintf(stderr,
  111. "\n"
  112. "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hvB] "
  113. "[-a<path>] \\\n"
  114. " [-G<ping interval>] [command..]\n"
  115. "\n"
  116. "Options:\n"
  117. " -h help (show this usage text)\n"
  118. " -v shown version information\n"
  119. " -p<path> path to find control sockets (default: "
  120. "/var/run/hostapd)\n"
  121. " -a<file> run in daemon mode executing the action file "
  122. "based on events\n"
  123. " from hostapd\n"
  124. " -B run a daemon in the background\n"
  125. " -i<ifname> Interface to listen on (default: first "
  126. "interface found in the\n"
  127. " socket path)\n\n"
  128. "%s",
  129. commands_help);
  130. }
  131. static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
  132. {
  133. char *cfile;
  134. int flen;
  135. if (ifname == NULL)
  136. return NULL;
  137. flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
  138. cfile = malloc(flen);
  139. if (cfile == NULL)
  140. return NULL;
  141. snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
  142. ctrl_conn = wpa_ctrl_open(cfile);
  143. free(cfile);
  144. return ctrl_conn;
  145. }
  146. static void hostapd_cli_close_connection(void)
  147. {
  148. if (ctrl_conn == NULL)
  149. return;
  150. if (hostapd_cli_attached) {
  151. wpa_ctrl_detach(ctrl_conn);
  152. hostapd_cli_attached = 0;
  153. }
  154. wpa_ctrl_close(ctrl_conn);
  155. ctrl_conn = NULL;
  156. }
  157. static void hostapd_cli_msg_cb(char *msg, size_t len)
  158. {
  159. printf("%s\n", msg);
  160. }
  161. static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
  162. {
  163. char buf[4096];
  164. size_t len;
  165. int ret;
  166. if (ctrl_conn == NULL) {
  167. printf("Not connected to hostapd - command dropped.\n");
  168. return -1;
  169. }
  170. len = sizeof(buf) - 1;
  171. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  172. hostapd_cli_msg_cb);
  173. if (ret == -2) {
  174. printf("'%s' command timed out.\n", cmd);
  175. return -2;
  176. } else if (ret < 0) {
  177. printf("'%s' command failed.\n", cmd);
  178. return -1;
  179. }
  180. if (print) {
  181. buf[len] = '\0';
  182. printf("%s", buf);
  183. }
  184. return 0;
  185. }
  186. static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
  187. {
  188. return _wpa_ctrl_command(ctrl, cmd, 1);
  189. }
  190. static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
  191. {
  192. return wpa_ctrl_command(ctrl, "PING");
  193. }
  194. static int hostapd_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
  195. {
  196. return wpa_ctrl_command(ctrl, "RELOG");
  197. }
  198. static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
  199. {
  200. return wpa_ctrl_command(ctrl, "MIB");
  201. }
  202. static int hostapd_cli_exec(const char *program, const char *arg1,
  203. const char *arg2)
  204. {
  205. char *cmd;
  206. size_t len;
  207. int res;
  208. int ret = 0;
  209. len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
  210. cmd = os_malloc(len);
  211. if (cmd == NULL)
  212. return -1;
  213. res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
  214. if (res < 0 || (size_t) res >= len) {
  215. os_free(cmd);
  216. return -1;
  217. }
  218. cmd[len - 1] = '\0';
  219. #ifndef _WIN32_WCE
  220. if (system(cmd) < 0)
  221. ret = -1;
  222. #endif /* _WIN32_WCE */
  223. os_free(cmd);
  224. return ret;
  225. }
  226. static void hostapd_cli_action_process(char *msg, size_t len)
  227. {
  228. const char *pos;
  229. pos = msg;
  230. if (*pos == '<') {
  231. pos = os_strchr(pos, '>');
  232. if (pos)
  233. pos++;
  234. else
  235. pos = msg;
  236. }
  237. hostapd_cli_exec(action_file, ctrl_ifname, pos);
  238. }
  239. static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  240. {
  241. char buf[64];
  242. if (argc != 1) {
  243. printf("Invalid 'sta' command - exactly one argument, STA "
  244. "address, is required.\n");
  245. return -1;
  246. }
  247. snprintf(buf, sizeof(buf), "STA %s", argv[0]);
  248. return wpa_ctrl_command(ctrl, buf);
  249. }
  250. static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
  251. char *argv[])
  252. {
  253. char buf[64];
  254. if (argc != 1) {
  255. printf("Invalid 'new_sta' command - exactly one argument, STA "
  256. "address, is required.\n");
  257. return -1;
  258. }
  259. snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
  260. return wpa_ctrl_command(ctrl, buf);
  261. }
  262. static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
  263. char *argv[])
  264. {
  265. char buf[64];
  266. if (argc < 1) {
  267. printf("Invalid 'deauthenticate' command - exactly one "
  268. "argument, STA address, is required.\n");
  269. return -1;
  270. }
  271. if (argc > 1)
  272. os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s %s",
  273. argv[0], argv[1]);
  274. else
  275. os_snprintf(buf, sizeof(buf), "DEAUTHENTICATE %s", argv[0]);
  276. return wpa_ctrl_command(ctrl, buf);
  277. }
  278. static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
  279. char *argv[])
  280. {
  281. char buf[64];
  282. if (argc < 1) {
  283. printf("Invalid 'disassociate' command - exactly one "
  284. "argument, STA address, is required.\n");
  285. return -1;
  286. }
  287. if (argc > 1)
  288. os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s %s",
  289. argv[0], argv[1]);
  290. else
  291. os_snprintf(buf, sizeof(buf), "DISASSOCIATE %s", argv[0]);
  292. return wpa_ctrl_command(ctrl, buf);
  293. }
  294. #ifdef CONFIG_IEEE80211W
  295. static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
  296. char *argv[])
  297. {
  298. char buf[64];
  299. if (argc != 1) {
  300. printf("Invalid 'sa_query' command - exactly one argument, "
  301. "STA address, is required.\n");
  302. return -1;
  303. }
  304. snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
  305. return wpa_ctrl_command(ctrl, buf);
  306. }
  307. #endif /* CONFIG_IEEE80211W */
  308. #ifdef CONFIG_WPS
  309. static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
  310. char *argv[])
  311. {
  312. char buf[256];
  313. if (argc < 2) {
  314. printf("Invalid 'wps_pin' command - at least two arguments, "
  315. "UUID and PIN, are required.\n");
  316. return -1;
  317. }
  318. if (argc > 3)
  319. snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s %s",
  320. argv[0], argv[1], argv[2], argv[3]);
  321. else if (argc > 2)
  322. snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
  323. argv[0], argv[1], argv[2]);
  324. else
  325. snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
  326. return wpa_ctrl_command(ctrl, buf);
  327. }
  328. static int hostapd_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
  329. char *argv[])
  330. {
  331. char cmd[256];
  332. int res;
  333. if (argc != 1 && argc != 2) {
  334. printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
  335. "- PIN to be verified\n");
  336. return -1;
  337. }
  338. if (argc == 2)
  339. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
  340. argv[0], argv[1]);
  341. else
  342. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
  343. argv[0]);
  344. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  345. printf("Too long WPS_CHECK_PIN command.\n");
  346. return -1;
  347. }
  348. return wpa_ctrl_command(ctrl, cmd);
  349. }
  350. static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
  351. char *argv[])
  352. {
  353. return wpa_ctrl_command(ctrl, "WPS_PBC");
  354. }
  355. #ifdef CONFIG_WPS_OOB
  356. static int hostapd_cli_cmd_wps_oob(struct wpa_ctrl *ctrl, int argc,
  357. char *argv[])
  358. {
  359. char cmd[256];
  360. int res;
  361. if (argc != 3 && argc != 4) {
  362. printf("Invalid WPS_OOB command: need three or four "
  363. "arguments:\n"
  364. "- DEV_TYPE: use 'ufd' or 'nfc'\n"
  365. "- PATH: path of OOB device like '/mnt'\n"
  366. "- METHOD: OOB method 'pin-e' or 'pin-r', "
  367. "'cred'\n"
  368. "- DEV_NAME: (only for NFC) device name like "
  369. "'pn531'\n");
  370. return -1;
  371. }
  372. if (argc == 3)
  373. res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s",
  374. argv[0], argv[1], argv[2]);
  375. else
  376. res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s %s",
  377. argv[0], argv[1], argv[2], argv[3]);
  378. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  379. printf("Too long WPS_OOB command.\n");
  380. return -1;
  381. }
  382. return wpa_ctrl_command(ctrl, cmd);
  383. }
  384. #endif /* CONFIG_WPS_OOB */
  385. static int hostapd_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
  386. char *argv[])
  387. {
  388. char buf[64];
  389. if (argc < 1) {
  390. printf("Invalid 'wps_ap_pin' command - at least one argument "
  391. "is required.\n");
  392. return -1;
  393. }
  394. if (argc > 2)
  395. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s %s",
  396. argv[0], argv[1], argv[2]);
  397. else if (argc > 1)
  398. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s %s",
  399. argv[0], argv[1]);
  400. else
  401. snprintf(buf, sizeof(buf), "WPS_AP_PIN %s", argv[0]);
  402. return wpa_ctrl_command(ctrl, buf);
  403. }
  404. static int hostapd_cli_cmd_wps_config(struct wpa_ctrl *ctrl, int argc,
  405. char *argv[])
  406. {
  407. char buf[256];
  408. char ssid_hex[2 * 32 + 1];
  409. char key_hex[2 * 64 + 1];
  410. int i;
  411. if (argc < 1) {
  412. printf("Invalid 'wps_config' command - at least two arguments "
  413. "are required.\n");
  414. return -1;
  415. }
  416. ssid_hex[0] = '\0';
  417. for (i = 0; i < 32; i++) {
  418. if (argv[0][i] == '\0')
  419. break;
  420. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[0][i]);
  421. }
  422. key_hex[0] = '\0';
  423. if (argc > 3) {
  424. for (i = 0; i < 64; i++) {
  425. if (argv[3][i] == '\0')
  426. break;
  427. os_snprintf(&key_hex[i * 2], 3, "%02x",
  428. argv[3][i]);
  429. }
  430. }
  431. if (argc > 3)
  432. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s %s",
  433. ssid_hex, argv[1], argv[2], key_hex);
  434. else if (argc > 2)
  435. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s %s",
  436. ssid_hex, argv[1], argv[2]);
  437. else
  438. snprintf(buf, sizeof(buf), "WPS_CONFIG %s %s",
  439. ssid_hex, argv[1]);
  440. return wpa_ctrl_command(ctrl, buf);
  441. }
  442. #endif /* CONFIG_WPS */
  443. static int hostapd_cli_cmd_get_config(struct wpa_ctrl *ctrl, int argc,
  444. char *argv[])
  445. {
  446. return wpa_ctrl_command(ctrl, "GET_CONFIG");
  447. }
  448. static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
  449. char *addr, size_t addr_len)
  450. {
  451. char buf[4096], *pos;
  452. size_t len;
  453. int ret;
  454. if (ctrl_conn == NULL) {
  455. printf("Not connected to hostapd - command dropped.\n");
  456. return -1;
  457. }
  458. len = sizeof(buf) - 1;
  459. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  460. hostapd_cli_msg_cb);
  461. if (ret == -2) {
  462. printf("'%s' command timed out.\n", cmd);
  463. return -2;
  464. } else if (ret < 0) {
  465. printf("'%s' command failed.\n", cmd);
  466. return -1;
  467. }
  468. buf[len] = '\0';
  469. if (memcmp(buf, "FAIL", 4) == 0)
  470. return -1;
  471. printf("%s", buf);
  472. pos = buf;
  473. while (*pos != '\0' && *pos != '\n')
  474. pos++;
  475. *pos = '\0';
  476. os_strlcpy(addr, buf, addr_len);
  477. return 0;
  478. }
  479. static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
  480. char *argv[])
  481. {
  482. char addr[32], cmd[64];
  483. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
  484. return 0;
  485. do {
  486. snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  487. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
  488. return -1;
  489. }
  490. static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
  491. {
  492. printf("%s", commands_help);
  493. return 0;
  494. }
  495. static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
  496. char *argv[])
  497. {
  498. printf("%s\n\n%s\n", hostapd_cli_version, hostapd_cli_full_license);
  499. return 0;
  500. }
  501. static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
  502. {
  503. hostapd_cli_quit = 1;
  504. return 0;
  505. }
  506. static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
  507. {
  508. char cmd[256];
  509. if (argc != 1) {
  510. printf("Invalid LEVEL command: needs one argument (debug "
  511. "level)\n");
  512. return 0;
  513. }
  514. snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
  515. return wpa_ctrl_command(ctrl, cmd);
  516. }
  517. static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
  518. {
  519. struct dirent *dent;
  520. DIR *dir;
  521. dir = opendir(ctrl_iface_dir);
  522. if (dir == NULL) {
  523. printf("Control interface directory '%s' could not be "
  524. "openned.\n", ctrl_iface_dir);
  525. return;
  526. }
  527. printf("Available interfaces:\n");
  528. while ((dent = readdir(dir))) {
  529. if (strcmp(dent->d_name, ".") == 0 ||
  530. strcmp(dent->d_name, "..") == 0)
  531. continue;
  532. printf("%s\n", dent->d_name);
  533. }
  534. closedir(dir);
  535. }
  536. static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
  537. char *argv[])
  538. {
  539. if (argc < 1) {
  540. hostapd_cli_list_interfaces(ctrl);
  541. return 0;
  542. }
  543. hostapd_cli_close_connection();
  544. free(ctrl_ifname);
  545. ctrl_ifname = strdup(argv[0]);
  546. if (hostapd_cli_open_connection(ctrl_ifname)) {
  547. printf("Connected to interface '%s.\n", ctrl_ifname);
  548. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  549. hostapd_cli_attached = 1;
  550. } else {
  551. printf("Warning: Failed to attach to "
  552. "hostapd.\n");
  553. }
  554. } else {
  555. printf("Could not connect to interface '%s' - re-trying\n",
  556. ctrl_ifname);
  557. }
  558. return 0;
  559. }
  560. static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  561. {
  562. char cmd[256];
  563. int res;
  564. if (argc != 2) {
  565. printf("Invalid SET command: needs two arguments (variable "
  566. "name and value)\n");
  567. return -1;
  568. }
  569. res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
  570. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  571. printf("Too long SET command.\n");
  572. return -1;
  573. }
  574. return wpa_ctrl_command(ctrl, cmd);
  575. }
  576. static int hostapd_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  577. {
  578. char cmd[256];
  579. int res;
  580. if (argc != 1) {
  581. printf("Invalid GET command: needs one argument (variable "
  582. "name)\n");
  583. return -1;
  584. }
  585. res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
  586. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  587. printf("Too long GET command.\n");
  588. return -1;
  589. }
  590. return wpa_ctrl_command(ctrl, cmd);
  591. }
  592. struct hostapd_cli_cmd {
  593. const char *cmd;
  594. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  595. };
  596. static struct hostapd_cli_cmd hostapd_cli_commands[] = {
  597. { "ping", hostapd_cli_cmd_ping },
  598. { "mib", hostapd_cli_cmd_mib },
  599. { "relog", hostapd_cli_cmd_relog },
  600. { "sta", hostapd_cli_cmd_sta },
  601. { "all_sta", hostapd_cli_cmd_all_sta },
  602. { "new_sta", hostapd_cli_cmd_new_sta },
  603. { "deauthenticate", hostapd_cli_cmd_deauthenticate },
  604. { "disassociate", hostapd_cli_cmd_disassociate },
  605. #ifdef CONFIG_IEEE80211W
  606. { "sa_query", hostapd_cli_cmd_sa_query },
  607. #endif /* CONFIG_IEEE80211W */
  608. #ifdef CONFIG_WPS
  609. { "wps_pin", hostapd_cli_cmd_wps_pin },
  610. { "wps_check_pin", hostapd_cli_cmd_wps_check_pin },
  611. { "wps_pbc", hostapd_cli_cmd_wps_pbc },
  612. #ifdef CONFIG_WPS_OOB
  613. { "wps_oob", hostapd_cli_cmd_wps_oob },
  614. #endif /* CONFIG_WPS_OOB */
  615. { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin },
  616. { "wps_config", hostapd_cli_cmd_wps_config },
  617. #endif /* CONFIG_WPS */
  618. { "get_config", hostapd_cli_cmd_get_config },
  619. { "help", hostapd_cli_cmd_help },
  620. { "interface", hostapd_cli_cmd_interface },
  621. { "level", hostapd_cli_cmd_level },
  622. { "license", hostapd_cli_cmd_license },
  623. { "quit", hostapd_cli_cmd_quit },
  624. { "set", hostapd_cli_cmd_set },
  625. { "get", hostapd_cli_cmd_get },
  626. { NULL, NULL }
  627. };
  628. static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  629. {
  630. struct hostapd_cli_cmd *cmd, *match = NULL;
  631. int count;
  632. count = 0;
  633. cmd = hostapd_cli_commands;
  634. while (cmd->cmd) {
  635. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
  636. match = cmd;
  637. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  638. /* we have an exact match */
  639. count = 1;
  640. break;
  641. }
  642. count++;
  643. }
  644. cmd++;
  645. }
  646. if (count > 1) {
  647. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  648. cmd = hostapd_cli_commands;
  649. while (cmd->cmd) {
  650. if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
  651. 0) {
  652. printf(" %s", cmd->cmd);
  653. }
  654. cmd++;
  655. }
  656. printf("\n");
  657. } else if (count == 0) {
  658. printf("Unknown command '%s'\n", argv[0]);
  659. } else {
  660. match->handler(ctrl, argc - 1, &argv[1]);
  661. }
  662. }
  663. static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
  664. int action_monitor)
  665. {
  666. int first = 1;
  667. if (ctrl_conn == NULL)
  668. return;
  669. while (wpa_ctrl_pending(ctrl)) {
  670. char buf[256];
  671. size_t len = sizeof(buf) - 1;
  672. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  673. buf[len] = '\0';
  674. if (action_monitor)
  675. hostapd_cli_action_process(buf, len);
  676. else {
  677. if (in_read && first)
  678. printf("\n");
  679. first = 0;
  680. printf("%s\n", buf);
  681. }
  682. } else {
  683. printf("Could not read pending message.\n");
  684. break;
  685. }
  686. }
  687. }
  688. static void hostapd_cli_interactive(void)
  689. {
  690. const int max_args = 10;
  691. char cmd[256], *res, *argv[max_args], *pos;
  692. int argc;
  693. printf("\nInteractive mode\n\n");
  694. do {
  695. hostapd_cli_recv_pending(ctrl_conn, 0, 0);
  696. printf("> ");
  697. alarm(ping_interval);
  698. res = fgets(cmd, sizeof(cmd), stdin);
  699. alarm(0);
  700. if (res == NULL)
  701. break;
  702. pos = cmd;
  703. while (*pos != '\0') {
  704. if (*pos == '\n') {
  705. *pos = '\0';
  706. break;
  707. }
  708. pos++;
  709. }
  710. argc = 0;
  711. pos = cmd;
  712. for (;;) {
  713. while (*pos == ' ')
  714. pos++;
  715. if (*pos == '\0')
  716. break;
  717. argv[argc] = pos;
  718. argc++;
  719. if (argc == max_args)
  720. break;
  721. while (*pos != '\0' && *pos != ' ')
  722. pos++;
  723. if (*pos == ' ')
  724. *pos++ = '\0';
  725. }
  726. if (argc)
  727. wpa_request(ctrl_conn, argc, argv);
  728. } while (!hostapd_cli_quit);
  729. }
  730. static void hostapd_cli_cleanup(void)
  731. {
  732. hostapd_cli_close_connection();
  733. if (pid_file)
  734. os_daemonize_terminate(pid_file);
  735. os_program_deinit();
  736. }
  737. static void hostapd_cli_terminate(int sig)
  738. {
  739. hostapd_cli_cleanup();
  740. exit(0);
  741. }
  742. static void hostapd_cli_alarm(int sig)
  743. {
  744. if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
  745. printf("Connection to hostapd lost - trying to reconnect\n");
  746. hostapd_cli_close_connection();
  747. }
  748. if (!ctrl_conn) {
  749. ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
  750. if (ctrl_conn) {
  751. printf("Connection to hostapd re-established\n");
  752. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  753. hostapd_cli_attached = 1;
  754. } else {
  755. printf("Warning: Failed to attach to "
  756. "hostapd.\n");
  757. }
  758. }
  759. }
  760. if (ctrl_conn)
  761. hostapd_cli_recv_pending(ctrl_conn, 1, 0);
  762. alarm(ping_interval);
  763. }
  764. static void hostapd_cli_action(struct wpa_ctrl *ctrl)
  765. {
  766. fd_set rfds;
  767. int fd, res;
  768. struct timeval tv;
  769. char buf[256];
  770. size_t len;
  771. fd = wpa_ctrl_get_fd(ctrl);
  772. while (!hostapd_cli_quit) {
  773. FD_ZERO(&rfds);
  774. FD_SET(fd, &rfds);
  775. tv.tv_sec = ping_interval;
  776. tv.tv_usec = 0;
  777. res = select(fd + 1, &rfds, NULL, NULL, &tv);
  778. if (res < 0 && errno != EINTR) {
  779. perror("select");
  780. break;
  781. }
  782. if (FD_ISSET(fd, &rfds))
  783. hostapd_cli_recv_pending(ctrl, 0, 1);
  784. else {
  785. len = sizeof(buf) - 1;
  786. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  787. hostapd_cli_action_process) < 0 ||
  788. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  789. printf("hostapd did not reply to PING "
  790. "command - exiting\n");
  791. break;
  792. }
  793. }
  794. }
  795. }
  796. int main(int argc, char *argv[])
  797. {
  798. int interactive;
  799. int warning_displayed = 0;
  800. int c;
  801. int daemonize = 0;
  802. if (os_program_init())
  803. return -1;
  804. for (;;) {
  805. c = getopt(argc, argv, "a:BhG:i:p:v");
  806. if (c < 0)
  807. break;
  808. switch (c) {
  809. case 'a':
  810. action_file = optarg;
  811. break;
  812. case 'B':
  813. daemonize = 1;
  814. break;
  815. case 'G':
  816. ping_interval = atoi(optarg);
  817. break;
  818. case 'h':
  819. usage();
  820. return 0;
  821. case 'v':
  822. printf("%s\n", hostapd_cli_version);
  823. return 0;
  824. case 'i':
  825. os_free(ctrl_ifname);
  826. ctrl_ifname = os_strdup(optarg);
  827. break;
  828. case 'p':
  829. ctrl_iface_dir = optarg;
  830. break;
  831. default:
  832. usage();
  833. return -1;
  834. }
  835. }
  836. interactive = (argc == optind) && (action_file == NULL);
  837. if (interactive) {
  838. printf("%s\n\n%s\n\n", hostapd_cli_version,
  839. hostapd_cli_license);
  840. }
  841. for (;;) {
  842. if (ctrl_ifname == NULL) {
  843. struct dirent *dent;
  844. DIR *dir = opendir(ctrl_iface_dir);
  845. if (dir) {
  846. while ((dent = readdir(dir))) {
  847. if (os_strcmp(dent->d_name, ".") == 0
  848. ||
  849. os_strcmp(dent->d_name, "..") == 0)
  850. continue;
  851. printf("Selected interface '%s'\n",
  852. dent->d_name);
  853. ctrl_ifname = os_strdup(dent->d_name);
  854. break;
  855. }
  856. closedir(dir);
  857. }
  858. }
  859. ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
  860. if (ctrl_conn) {
  861. if (warning_displayed)
  862. printf("Connection established.\n");
  863. break;
  864. }
  865. if (!interactive) {
  866. perror("Failed to connect to hostapd - "
  867. "wpa_ctrl_open");
  868. return -1;
  869. }
  870. if (!warning_displayed) {
  871. printf("Could not connect to hostapd - re-trying\n");
  872. warning_displayed = 1;
  873. }
  874. os_sleep(1, 0);
  875. continue;
  876. }
  877. signal(SIGINT, hostapd_cli_terminate);
  878. signal(SIGTERM, hostapd_cli_terminate);
  879. signal(SIGALRM, hostapd_cli_alarm);
  880. if (interactive || action_file) {
  881. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  882. hostapd_cli_attached = 1;
  883. } else {
  884. printf("Warning: Failed to attach to hostapd.\n");
  885. if (action_file)
  886. return -1;
  887. }
  888. }
  889. if (daemonize && os_daemonize(pid_file))
  890. return -1;
  891. if (interactive)
  892. hostapd_cli_interactive();
  893. else if (action_file)
  894. hostapd_cli_action(ctrl_conn);
  895. else
  896. wpa_request(ctrl_conn, argc - optind, &argv[optind]);
  897. os_free(ctrl_ifname);
  898. hostapd_cli_cleanup();
  899. return 0;
  900. }