wpa_ctrl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * wpa_supplicant/hostapd control interface library
  3. * Copyright (c) 2004-2007, 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. #ifdef CONFIG_CTRL_IFACE
  16. #ifdef CONFIG_CTRL_IFACE_UNIX
  17. #include <sys/un.h>
  18. #endif /* CONFIG_CTRL_IFACE_UNIX */
  19. #include "wpa_ctrl.h"
  20. #include "common.h"
  21. #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
  22. #define CTRL_IFACE_SOCKET
  23. #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
  24. /**
  25. * struct wpa_ctrl - Internal structure for control interface library
  26. *
  27. * This structure is used by the wpa_supplicant/hostapd control interface
  28. * library to store internal data. Programs using the library should not touch
  29. * this data directly. They can only use the pointer to the data structure as
  30. * an identifier for the control interface connection and use this as one of
  31. * the arguments for most of the control interface library functions.
  32. */
  33. struct wpa_ctrl {
  34. #ifdef CONFIG_CTRL_IFACE_UDP
  35. int s;
  36. struct sockaddr_in local;
  37. struct sockaddr_in dest;
  38. char *cookie;
  39. #endif /* CONFIG_CTRL_IFACE_UDP */
  40. #ifdef CONFIG_CTRL_IFACE_UNIX
  41. int s;
  42. struct sockaddr_un local;
  43. struct sockaddr_un dest;
  44. #endif /* CONFIG_CTRL_IFACE_UNIX */
  45. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  46. HANDLE pipe;
  47. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  48. };
  49. #ifdef CONFIG_CTRL_IFACE_UNIX
  50. struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
  51. {
  52. struct wpa_ctrl *ctrl;
  53. static int counter = 0;
  54. int ret;
  55. size_t res;
  56. int tries = 0;
  57. ctrl = os_malloc(sizeof(*ctrl));
  58. if (ctrl == NULL)
  59. return NULL;
  60. os_memset(ctrl, 0, sizeof(*ctrl));
  61. ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
  62. if (ctrl->s < 0) {
  63. os_free(ctrl);
  64. return NULL;
  65. }
  66. ctrl->local.sun_family = AF_UNIX;
  67. counter++;
  68. try_again:
  69. ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
  70. "/tmp/wpa_ctrl_%d-%d", (int) getpid(), counter);
  71. if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
  72. close(ctrl->s);
  73. os_free(ctrl);
  74. return NULL;
  75. }
  76. tries++;
  77. if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
  78. sizeof(ctrl->local)) < 0) {
  79. if (errno == EADDRINUSE && tries < 2) {
  80. /*
  81. * getpid() returns unique identifier for this instance
  82. * of wpa_ctrl, so the existing socket file must have
  83. * been left by unclean termination of an earlier run.
  84. * Remove the file and try again.
  85. */
  86. unlink(ctrl->local.sun_path);
  87. goto try_again;
  88. }
  89. close(ctrl->s);
  90. os_free(ctrl);
  91. return NULL;
  92. }
  93. ctrl->dest.sun_family = AF_UNIX;
  94. res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
  95. sizeof(ctrl->dest.sun_path));
  96. if (res >= sizeof(ctrl->dest.sun_path)) {
  97. close(ctrl->s);
  98. os_free(ctrl);
  99. return NULL;
  100. }
  101. if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
  102. sizeof(ctrl->dest)) < 0) {
  103. close(ctrl->s);
  104. unlink(ctrl->local.sun_path);
  105. os_free(ctrl);
  106. return NULL;
  107. }
  108. return ctrl;
  109. }
  110. void wpa_ctrl_close(struct wpa_ctrl *ctrl)
  111. {
  112. unlink(ctrl->local.sun_path);
  113. close(ctrl->s);
  114. os_free(ctrl);
  115. }
  116. #endif /* CONFIG_CTRL_IFACE_UNIX */
  117. #ifdef CONFIG_CTRL_IFACE_UDP
  118. struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
  119. {
  120. struct wpa_ctrl *ctrl;
  121. char buf[128];
  122. size_t len;
  123. ctrl = os_malloc(sizeof(*ctrl));
  124. if (ctrl == NULL)
  125. return NULL;
  126. os_memset(ctrl, 0, sizeof(*ctrl));
  127. ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
  128. if (ctrl->s < 0) {
  129. perror("socket");
  130. os_free(ctrl);
  131. return NULL;
  132. }
  133. ctrl->local.sin_family = AF_INET;
  134. ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
  135. if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
  136. sizeof(ctrl->local)) < 0) {
  137. close(ctrl->s);
  138. os_free(ctrl);
  139. return NULL;
  140. }
  141. ctrl->dest.sin_family = AF_INET;
  142. ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
  143. ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
  144. if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
  145. sizeof(ctrl->dest)) < 0) {
  146. perror("connect");
  147. close(ctrl->s);
  148. os_free(ctrl);
  149. return NULL;
  150. }
  151. len = sizeof(buf) - 1;
  152. if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
  153. buf[len] = '\0';
  154. ctrl->cookie = os_strdup(buf);
  155. }
  156. return ctrl;
  157. }
  158. void wpa_ctrl_close(struct wpa_ctrl *ctrl)
  159. {
  160. close(ctrl->s);
  161. os_free(ctrl->cookie);
  162. os_free(ctrl);
  163. }
  164. #endif /* CONFIG_CTRL_IFACE_UDP */
  165. #ifdef CTRL_IFACE_SOCKET
  166. int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
  167. char *reply, size_t *reply_len,
  168. void (*msg_cb)(char *msg, size_t len))
  169. {
  170. struct timeval tv;
  171. int res;
  172. fd_set rfds;
  173. const char *_cmd;
  174. char *cmd_buf = NULL;
  175. size_t _cmd_len;
  176. #ifdef CONFIG_CTRL_IFACE_UDP
  177. if (ctrl->cookie) {
  178. char *pos;
  179. _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
  180. cmd_buf = os_malloc(_cmd_len);
  181. if (cmd_buf == NULL)
  182. return -1;
  183. _cmd = cmd_buf;
  184. pos = cmd_buf;
  185. os_strlcpy(pos, ctrl->cookie, _cmd_len);
  186. pos += os_strlen(ctrl->cookie);
  187. *pos++ = ' ';
  188. os_memcpy(pos, cmd, cmd_len);
  189. } else
  190. #endif /* CONFIG_CTRL_IFACE_UDP */
  191. {
  192. _cmd = cmd;
  193. _cmd_len = cmd_len;
  194. }
  195. if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
  196. os_free(cmd_buf);
  197. return -1;
  198. }
  199. os_free(cmd_buf);
  200. for (;;) {
  201. tv.tv_sec = 2;
  202. tv.tv_usec = 0;
  203. FD_ZERO(&rfds);
  204. FD_SET(ctrl->s, &rfds);
  205. res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
  206. if (FD_ISSET(ctrl->s, &rfds)) {
  207. res = recv(ctrl->s, reply, *reply_len, 0);
  208. if (res < 0)
  209. return res;
  210. if (res > 0 && reply[0] == '<') {
  211. /* This is an unsolicited message from
  212. * wpa_supplicant, not the reply to the
  213. * request. Use msg_cb to report this to the
  214. * caller. */
  215. if (msg_cb) {
  216. /* Make sure the message is nul
  217. * terminated. */
  218. if ((size_t) res == *reply_len)
  219. res = (*reply_len) - 1;
  220. reply[res] = '\0';
  221. msg_cb(reply, res);
  222. }
  223. continue;
  224. }
  225. *reply_len = res;
  226. break;
  227. } else {
  228. return -2;
  229. }
  230. }
  231. return 0;
  232. }
  233. #endif /* CTRL_IFACE_SOCKET */
  234. static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
  235. {
  236. char buf[10];
  237. int ret;
  238. size_t len = 10;
  239. ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
  240. buf, &len, NULL);
  241. if (ret < 0)
  242. return ret;
  243. if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
  244. return 0;
  245. return -1;
  246. }
  247. int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
  248. {
  249. return wpa_ctrl_attach_helper(ctrl, 1);
  250. }
  251. int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
  252. {
  253. return wpa_ctrl_attach_helper(ctrl, 0);
  254. }
  255. #ifdef CTRL_IFACE_SOCKET
  256. int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
  257. {
  258. int res;
  259. res = recv(ctrl->s, reply, *reply_len, 0);
  260. if (res < 0)
  261. return res;
  262. *reply_len = res;
  263. return 0;
  264. }
  265. int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
  266. {
  267. struct timeval tv;
  268. fd_set rfds;
  269. tv.tv_sec = 0;
  270. tv.tv_usec = 0;
  271. FD_ZERO(&rfds);
  272. FD_SET(ctrl->s, &rfds);
  273. select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
  274. return FD_ISSET(ctrl->s, &rfds);
  275. }
  276. int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
  277. {
  278. return ctrl->s;
  279. }
  280. #endif /* CTRL_IFACE_SOCKET */
  281. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  282. #ifndef WPA_SUPPLICANT_NAMED_PIPE
  283. #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
  284. #endif
  285. #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
  286. struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
  287. {
  288. struct wpa_ctrl *ctrl;
  289. DWORD mode;
  290. TCHAR name[256];
  291. int i, ret;
  292. ctrl = os_malloc(sizeof(*ctrl));
  293. if (ctrl == NULL)
  294. return NULL;
  295. os_memset(ctrl, 0, sizeof(*ctrl));
  296. #ifdef UNICODE
  297. if (ctrl_path == NULL)
  298. ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
  299. else
  300. ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
  301. ctrl_path);
  302. #else /* UNICODE */
  303. if (ctrl_path == NULL)
  304. ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
  305. else
  306. ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
  307. ctrl_path);
  308. #endif /* UNICODE */
  309. if (ret < 0 || ret >= 256) {
  310. os_free(ctrl);
  311. return NULL;
  312. }
  313. for (i = 0; i < 10; i++) {
  314. ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
  315. NULL, OPEN_EXISTING, 0, NULL);
  316. /*
  317. * Current named pipe server side in wpa_supplicant is
  318. * re-opening the pipe for new clients only after the previous
  319. * one is taken into use. This leaves a small window for race
  320. * conditions when two connections are being opened at almost
  321. * the same time. Retry if that was the case.
  322. */
  323. if (ctrl->pipe != INVALID_HANDLE_VALUE ||
  324. GetLastError() != ERROR_PIPE_BUSY)
  325. break;
  326. WaitNamedPipe(name, 1000);
  327. }
  328. if (ctrl->pipe == INVALID_HANDLE_VALUE) {
  329. os_free(ctrl);
  330. return NULL;
  331. }
  332. mode = PIPE_READMODE_MESSAGE;
  333. if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
  334. CloseHandle(ctrl->pipe);
  335. os_free(ctrl);
  336. return NULL;
  337. }
  338. return ctrl;
  339. }
  340. void wpa_ctrl_close(struct wpa_ctrl *ctrl)
  341. {
  342. CloseHandle(ctrl->pipe);
  343. os_free(ctrl);
  344. }
  345. int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
  346. char *reply, size_t *reply_len,
  347. void (*msg_cb)(char *msg, size_t len))
  348. {
  349. DWORD written;
  350. DWORD readlen = *reply_len;
  351. if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
  352. return -1;
  353. if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
  354. return -1;
  355. *reply_len = readlen;
  356. return 0;
  357. }
  358. int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
  359. {
  360. DWORD len = *reply_len;
  361. if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
  362. return -1;
  363. *reply_len = len;
  364. return 0;
  365. }
  366. int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
  367. {
  368. DWORD left;
  369. if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
  370. return -1;
  371. return left ? 1 : 0;
  372. }
  373. int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
  374. {
  375. return -1;
  376. }
  377. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  378. #endif /* CONFIG_CTRL_IFACE */