ctrl_iface_named_pipe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * WPA Supplicant / Windows Named Pipe -based control interface
  3. * Copyright (c) 2004-2006, 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 "common.h"
  10. #include "eloop.h"
  11. #include "config.h"
  12. #include "eapol_supp/eapol_supp_sm.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "ctrl_iface.h"
  15. #include "common/wpa_ctrl.h"
  16. #ifdef __MINGW32_VERSION
  17. /* mingw-w32api v3.1 does not yet include sddl.h, so define needed parts here
  18. */
  19. #define SDDL_REVISION_1 1
  20. BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorA(
  21. LPCSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
  22. BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorW(
  23. LPCWSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
  24. #ifdef UNICODE
  25. #define ConvertStringSecurityDescriptorToSecurityDescriptor \
  26. ConvertStringSecurityDescriptorToSecurityDescriptorW
  27. #else
  28. #define ConvertStringSecurityDescriptorToSecurityDescriptor \
  29. ConvertStringSecurityDescriptorToSecurityDescriptorA
  30. #endif
  31. #else /* __MINGW32_VERSION */
  32. #ifndef _WIN32_WINNT
  33. #define _WIN32_WINNT 0x0500
  34. #endif
  35. #include <sddl.h>
  36. #endif /* __MINGW32_VERSION */
  37. #ifndef WPA_SUPPLICANT_NAMED_PIPE
  38. #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
  39. #endif
  40. #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
  41. /* Per-interface ctrl_iface */
  42. #define REQUEST_BUFSIZE 256
  43. #define REPLY_BUFSIZE 4096
  44. struct ctrl_iface_priv;
  45. /**
  46. * struct wpa_ctrl_dst - Internal data structure of control interface clients
  47. *
  48. * This structure is used to store information about registered control
  49. * interface monitors into struct wpa_supplicant. This data is private to
  50. * ctrl_iface_named_pipe.c and should not be touched directly from other files.
  51. */
  52. struct wpa_ctrl_dst {
  53. /* Note: OVERLAPPED must be the first member of struct wpa_ctrl_dst */
  54. OVERLAPPED overlap;
  55. struct wpa_ctrl_dst *next, *prev;
  56. struct ctrl_iface_priv *priv;
  57. HANDLE pipe;
  58. int attached;
  59. int debug_level;
  60. int errors;
  61. char req_buf[REQUEST_BUFSIZE];
  62. char *rsp_buf;
  63. int used;
  64. };
  65. struct ctrl_iface_priv {
  66. struct wpa_supplicant *wpa_s;
  67. struct wpa_ctrl_dst *ctrl_dst;
  68. SECURITY_ATTRIBUTES attr;
  69. int sec_attr_set;
  70. };
  71. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  72. int level, const char *buf,
  73. size_t len);
  74. static void ctrl_close_pipe(struct wpa_ctrl_dst *dst);
  75. static void wpa_supplicant_ctrl_iface_receive(void *, void *);
  76. static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
  77. LPOVERLAPPED overlap);
  78. struct wpa_global_dst;
  79. static void global_close_pipe(struct wpa_global_dst *dst);
  80. static void wpa_supplicant_global_iface_receive(void *eloop_data,
  81. void *user_ctx);
  82. static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
  83. LPOVERLAPPED overlap);
  84. static int ctrl_broken_pipe(HANDLE pipe, int used)
  85. {
  86. DWORD err;
  87. if (PeekNamedPipe(pipe, NULL, 0, NULL, NULL, NULL))
  88. return 0;
  89. err = GetLastError();
  90. if (err == ERROR_BROKEN_PIPE || (err == ERROR_BAD_PIPE && used))
  91. return 1;
  92. return 0;
  93. }
  94. static void ctrl_flush_broken_pipes(struct ctrl_iface_priv *priv)
  95. {
  96. struct wpa_ctrl_dst *dst, *next;
  97. dst = priv->ctrl_dst;
  98. while (dst) {
  99. next = dst->next;
  100. if (ctrl_broken_pipe(dst->pipe, dst->used)) {
  101. wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
  102. dst);
  103. ctrl_close_pipe(dst);
  104. }
  105. dst = next;
  106. }
  107. }
  108. static int ctrl_open_pipe(struct ctrl_iface_priv *priv)
  109. {
  110. struct wpa_ctrl_dst *dst;
  111. DWORD err;
  112. TCHAR name[256];
  113. dst = os_zalloc(sizeof(*dst));
  114. if (dst == NULL)
  115. return -1;
  116. wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
  117. dst->priv = priv;
  118. dst->debug_level = MSG_INFO;
  119. dst->pipe = INVALID_HANDLE_VALUE;
  120. dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
  121. if (dst->overlap.hEvent == NULL) {
  122. wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
  123. (int) GetLastError());
  124. goto fail;
  125. }
  126. eloop_register_event(dst->overlap.hEvent,
  127. sizeof(dst->overlap.hEvent),
  128. wpa_supplicant_ctrl_iface_receive, dst, NULL);
  129. #ifdef UNICODE
  130. _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
  131. priv->wpa_s->ifname);
  132. #else /* UNICODE */
  133. os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
  134. priv->wpa_s->ifname);
  135. #endif /* UNICODE */
  136. /* TODO: add support for configuring access list for the pipe */
  137. dst->pipe = CreateNamedPipe(name,
  138. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  139. PIPE_TYPE_MESSAGE |
  140. PIPE_READMODE_MESSAGE |
  141. PIPE_WAIT,
  142. 15, REPLY_BUFSIZE, REQUEST_BUFSIZE,
  143. 1000,
  144. priv->sec_attr_set ? &priv->attr : NULL);
  145. if (dst->pipe == INVALID_HANDLE_VALUE) {
  146. wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
  147. (int) GetLastError());
  148. goto fail;
  149. }
  150. if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
  151. wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
  152. (int) GetLastError());
  153. CloseHandle(dst->pipe);
  154. os_free(dst);
  155. return -1;
  156. }
  157. err = GetLastError();
  158. switch (err) {
  159. case ERROR_IO_PENDING:
  160. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
  161. "progress");
  162. break;
  163. case ERROR_PIPE_CONNECTED:
  164. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
  165. "connected");
  166. if (SetEvent(dst->overlap.hEvent))
  167. break;
  168. /* fall through */
  169. default:
  170. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
  171. (int) err);
  172. CloseHandle(dst->pipe);
  173. os_free(dst);
  174. return -1;
  175. }
  176. dst->next = priv->ctrl_dst;
  177. if (dst->next)
  178. dst->next->prev = dst;
  179. priv->ctrl_dst = dst;
  180. return 0;
  181. fail:
  182. ctrl_close_pipe(dst);
  183. return -1;
  184. }
  185. static void ctrl_close_pipe(struct wpa_ctrl_dst *dst)
  186. {
  187. wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
  188. if (dst->overlap.hEvent) {
  189. eloop_unregister_event(dst->overlap.hEvent,
  190. sizeof(dst->overlap.hEvent));
  191. CloseHandle(dst->overlap.hEvent);
  192. }
  193. if (dst->pipe != INVALID_HANDLE_VALUE) {
  194. /*
  195. * Could use FlushFileBuffers() here to guarantee that all data
  196. * gets delivered to the client, but that can block, so let's
  197. * not do this for now.
  198. * FlushFileBuffers(dst->pipe);
  199. */
  200. CloseHandle(dst->pipe);
  201. }
  202. if (dst->prev)
  203. dst->prev->next = dst->next;
  204. else
  205. dst->priv->ctrl_dst = dst->next;
  206. if (dst->next)
  207. dst->next->prev = dst->prev;
  208. os_free(dst->rsp_buf);
  209. os_free(dst);
  210. }
  211. static VOID WINAPI ctrl_iface_write_completed(DWORD err, DWORD bytes,
  212. LPOVERLAPPED overlap)
  213. {
  214. struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
  215. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
  216. "err=%d bytes=%d", dst, (int) err, (int) bytes);
  217. if (err) {
  218. ctrl_close_pipe(dst);
  219. return;
  220. }
  221. os_free(dst->rsp_buf);
  222. dst->rsp_buf = NULL;
  223. if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
  224. &dst->overlap, ctrl_iface_read_completed)) {
  225. wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
  226. (int) GetLastError());
  227. ctrl_close_pipe(dst);
  228. return;
  229. }
  230. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
  231. }
  232. static void wpa_supplicant_ctrl_iface_rx(struct wpa_ctrl_dst *dst, size_t len)
  233. {
  234. struct wpa_supplicant *wpa_s = dst->priv->wpa_s;
  235. char *reply = NULL, *send_buf;
  236. size_t reply_len = 0, send_len;
  237. int new_attached = 0;
  238. char *buf = dst->req_buf;
  239. dst->used = 1;
  240. if (len >= REQUEST_BUFSIZE)
  241. len = REQUEST_BUFSIZE - 1;
  242. buf[len] = '\0';
  243. if (os_strcmp(buf, "ATTACH") == 0) {
  244. dst->attached = 1;
  245. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached");
  246. new_attached = 1;
  247. reply_len = 2;
  248. } else if (os_strcmp(buf, "DETACH") == 0) {
  249. dst->attached = 0;
  250. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached");
  251. reply_len = 2;
  252. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  253. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", buf + 6);
  254. dst->debug_level = atoi(buf + 6);
  255. reply_len = 2;
  256. } else {
  257. reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
  258. &reply_len);
  259. }
  260. if (reply) {
  261. send_buf = reply;
  262. send_len = reply_len;
  263. } else if (reply_len == 2) {
  264. send_buf = "OK\n";
  265. send_len = 3;
  266. } else {
  267. send_buf = "FAIL\n";
  268. send_len = 5;
  269. }
  270. os_free(dst->rsp_buf);
  271. dst->rsp_buf = os_malloc(send_len);
  272. if (dst->rsp_buf == NULL) {
  273. ctrl_close_pipe(dst);
  274. os_free(reply);
  275. return;
  276. }
  277. os_memcpy(dst->rsp_buf, send_buf, send_len);
  278. os_free(reply);
  279. if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
  280. ctrl_iface_write_completed)) {
  281. wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
  282. (int) GetLastError());
  283. ctrl_close_pipe(dst);
  284. } else {
  285. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
  286. dst);
  287. }
  288. if (new_attached)
  289. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  290. }
  291. static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
  292. LPOVERLAPPED overlap)
  293. {
  294. struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
  295. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
  296. "bytes=%d", dst, (int) err, (int) bytes);
  297. if (err == 0 && bytes > 0)
  298. wpa_supplicant_ctrl_iface_rx(dst, bytes);
  299. }
  300. static void wpa_supplicant_ctrl_iface_receive(void *eloop_data, void *user_ctx)
  301. {
  302. struct wpa_ctrl_dst *dst = eloop_data;
  303. struct ctrl_iface_priv *priv = dst->priv;
  304. DWORD bytes;
  305. wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_ctrl_iface_receive");
  306. ResetEvent(dst->overlap.hEvent);
  307. if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
  308. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
  309. (int) GetLastError());
  310. return;
  311. }
  312. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
  313. "connected");
  314. /* Open a new named pipe for the next client. */
  315. ctrl_open_pipe(priv);
  316. /* Use write completion function to start reading a command */
  317. ctrl_iface_write_completed(0, 0, &dst->overlap);
  318. ctrl_flush_broken_pipes(priv);
  319. }
  320. static int ctrl_iface_parse(struct ctrl_iface_priv *priv, const char *params)
  321. {
  322. const char *sddl = NULL;
  323. TCHAR *t_sddl;
  324. if (os_strncmp(params, "SDDL=", 5) == 0)
  325. sddl = params + 5;
  326. if (!sddl) {
  327. sddl = os_strstr(params, " SDDL=");
  328. if (sddl)
  329. sddl += 6;
  330. }
  331. if (!sddl)
  332. return 0;
  333. wpa_printf(MSG_DEBUG, "CTRL: SDDL='%s'", sddl);
  334. os_memset(&priv->attr, 0, sizeof(priv->attr));
  335. priv->attr.nLength = sizeof(priv->attr);
  336. priv->attr.bInheritHandle = FALSE;
  337. t_sddl = wpa_strdup_tchar(sddl);
  338. if (t_sddl == NULL)
  339. return -1;
  340. if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
  341. t_sddl, SDDL_REVISION_1,
  342. (PSECURITY_DESCRIPTOR *) (void *)
  343. &priv->attr.lpSecurityDescriptor,
  344. NULL)) {
  345. os_free(t_sddl);
  346. wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
  347. "security descriptor: %d",
  348. sddl, (int) GetLastError());
  349. return -1;
  350. }
  351. os_free(t_sddl);
  352. priv->sec_attr_set = 1;
  353. return 0;
  354. }
  355. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  356. enum wpa_msg_type type,
  357. const char *txt, size_t len)
  358. {
  359. struct wpa_supplicant *wpa_s = ctx;
  360. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  361. return;
  362. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  363. }
  364. struct ctrl_iface_priv *
  365. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  366. {
  367. struct ctrl_iface_priv *priv;
  368. priv = os_zalloc(sizeof(*priv));
  369. if (priv == NULL)
  370. return NULL;
  371. priv->wpa_s = wpa_s;
  372. if (wpa_s->conf->ctrl_interface == NULL)
  373. return priv;
  374. if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
  375. os_free(priv);
  376. return NULL;
  377. }
  378. if (ctrl_open_pipe(priv) < 0) {
  379. os_free(priv);
  380. return NULL;
  381. }
  382. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  383. return priv;
  384. }
  385. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  386. {
  387. while (priv->ctrl_dst)
  388. ctrl_close_pipe(priv->ctrl_dst);
  389. if (priv->sec_attr_set)
  390. LocalFree(priv->attr.lpSecurityDescriptor);
  391. os_free(priv);
  392. }
  393. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  394. int level, const char *buf,
  395. size_t len)
  396. {
  397. struct wpa_ctrl_dst *dst, *next;
  398. char levelstr[10];
  399. int idx;
  400. char *sbuf;
  401. int llen;
  402. DWORD written;
  403. dst = priv->ctrl_dst;
  404. if (dst == NULL)
  405. return;
  406. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  407. llen = os_strlen(levelstr);
  408. sbuf = os_malloc(llen + len);
  409. if (sbuf == NULL)
  410. return;
  411. os_memcpy(sbuf, levelstr, llen);
  412. os_memcpy(sbuf + llen, buf, len);
  413. idx = 0;
  414. while (dst) {
  415. next = dst->next;
  416. if (dst->attached && level >= dst->debug_level) {
  417. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
  418. dst);
  419. if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
  420. NULL)) {
  421. wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
  422. "%p failed: %d",
  423. dst, (int) GetLastError());
  424. dst->errors++;
  425. if (dst->errors > 10)
  426. ctrl_close_pipe(dst);
  427. } else
  428. dst->errors = 0;
  429. }
  430. idx++;
  431. dst = next;
  432. }
  433. os_free(sbuf);
  434. }
  435. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  436. {
  437. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
  438. priv->wpa_s->ifname);
  439. if (priv->ctrl_dst == NULL)
  440. return;
  441. WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
  442. }
  443. /* Global ctrl_iface */
  444. struct ctrl_iface_global_priv;
  445. struct wpa_global_dst {
  446. /* Note: OVERLAPPED must be the first member of struct wpa_global_dst
  447. */
  448. OVERLAPPED overlap;
  449. struct wpa_global_dst *next, *prev;
  450. struct ctrl_iface_global_priv *priv;
  451. HANDLE pipe;
  452. char req_buf[REQUEST_BUFSIZE];
  453. char *rsp_buf;
  454. int used;
  455. };
  456. struct ctrl_iface_global_priv {
  457. struct wpa_global *global;
  458. struct wpa_global_dst *ctrl_dst;
  459. };
  460. static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
  461. {
  462. struct wpa_global_dst *dst, *next;
  463. dst = priv->ctrl_dst;
  464. while (dst) {
  465. next = dst->next;
  466. if (ctrl_broken_pipe(dst->pipe, dst->used)) {
  467. wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
  468. dst);
  469. global_close_pipe(dst);
  470. }
  471. dst = next;
  472. }
  473. }
  474. static int global_open_pipe(struct ctrl_iface_global_priv *priv)
  475. {
  476. struct wpa_global_dst *dst;
  477. DWORD err;
  478. dst = os_zalloc(sizeof(*dst));
  479. if (dst == NULL)
  480. return -1;
  481. wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
  482. dst->priv = priv;
  483. dst->pipe = INVALID_HANDLE_VALUE;
  484. dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
  485. if (dst->overlap.hEvent == NULL) {
  486. wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
  487. (int) GetLastError());
  488. goto fail;
  489. }
  490. eloop_register_event(dst->overlap.hEvent,
  491. sizeof(dst->overlap.hEvent),
  492. wpa_supplicant_global_iface_receive, dst, NULL);
  493. /* TODO: add support for configuring access list for the pipe */
  494. dst->pipe = CreateNamedPipe(NAMED_PIPE_PREFIX,
  495. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  496. PIPE_TYPE_MESSAGE |
  497. PIPE_READMODE_MESSAGE |
  498. PIPE_WAIT,
  499. 10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
  500. 1000, NULL);
  501. if (dst->pipe == INVALID_HANDLE_VALUE) {
  502. wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
  503. (int) GetLastError());
  504. goto fail;
  505. }
  506. if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
  507. wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
  508. (int) GetLastError());
  509. CloseHandle(dst->pipe);
  510. os_free(dst);
  511. return -1;
  512. }
  513. err = GetLastError();
  514. switch (err) {
  515. case ERROR_IO_PENDING:
  516. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
  517. "progress");
  518. break;
  519. case ERROR_PIPE_CONNECTED:
  520. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
  521. "connected");
  522. if (SetEvent(dst->overlap.hEvent))
  523. break;
  524. /* fall through */
  525. default:
  526. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
  527. (int) err);
  528. CloseHandle(dst->pipe);
  529. os_free(dst);
  530. return -1;
  531. }
  532. dst->next = priv->ctrl_dst;
  533. if (dst->next)
  534. dst->next->prev = dst;
  535. priv->ctrl_dst = dst;
  536. return 0;
  537. fail:
  538. global_close_pipe(dst);
  539. return -1;
  540. }
  541. static void global_close_pipe(struct wpa_global_dst *dst)
  542. {
  543. wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
  544. if (dst->overlap.hEvent) {
  545. eloop_unregister_event(dst->overlap.hEvent,
  546. sizeof(dst->overlap.hEvent));
  547. CloseHandle(dst->overlap.hEvent);
  548. }
  549. if (dst->pipe != INVALID_HANDLE_VALUE) {
  550. /*
  551. * Could use FlushFileBuffers() here to guarantee that all data
  552. * gets delivered to the client, but that can block, so let's
  553. * not do this for now.
  554. * FlushFileBuffers(dst->pipe);
  555. */
  556. CloseHandle(dst->pipe);
  557. }
  558. if (dst->prev)
  559. dst->prev->next = dst->next;
  560. else
  561. dst->priv->ctrl_dst = dst->next;
  562. if (dst->next)
  563. dst->next->prev = dst->prev;
  564. os_free(dst->rsp_buf);
  565. os_free(dst);
  566. }
  567. static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
  568. LPOVERLAPPED overlap)
  569. {
  570. struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
  571. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
  572. "err=%d bytes=%d", dst, (int) err, (int) bytes);
  573. if (err) {
  574. global_close_pipe(dst);
  575. return;
  576. }
  577. os_free(dst->rsp_buf);
  578. dst->rsp_buf = NULL;
  579. if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
  580. &dst->overlap, global_iface_read_completed)) {
  581. wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
  582. (int) GetLastError());
  583. global_close_pipe(dst);
  584. /* FIX: if this was the pipe waiting for new global
  585. * connections, at this point there are no open global pipes..
  586. * Should try to open a new pipe.. */
  587. return;
  588. }
  589. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
  590. }
  591. static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
  592. size_t len)
  593. {
  594. struct wpa_global *global = dst->priv->global;
  595. char *reply = NULL, *send_buf;
  596. size_t reply_len = 0, send_len;
  597. char *buf = dst->req_buf;
  598. dst->used = 1;
  599. if (len >= REQUEST_BUFSIZE)
  600. len = REQUEST_BUFSIZE - 1;
  601. buf[len] = '\0';
  602. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  603. &reply_len);
  604. if (reply) {
  605. send_buf = reply;
  606. send_len = reply_len;
  607. } else if (reply_len) {
  608. send_buf = "FAIL\n";
  609. send_len = 5;
  610. } else {
  611. os_free(dst->rsp_buf);
  612. dst->rsp_buf = NULL;
  613. return;
  614. }
  615. os_free(dst->rsp_buf);
  616. dst->rsp_buf = os_malloc(send_len);
  617. if (dst->rsp_buf == NULL) {
  618. global_close_pipe(dst);
  619. os_free(reply);
  620. return;
  621. }
  622. os_memcpy(dst->rsp_buf, send_buf, send_len);
  623. os_free(reply);
  624. if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
  625. global_iface_write_completed)) {
  626. wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
  627. (int) GetLastError());
  628. global_close_pipe(dst);
  629. } else {
  630. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
  631. dst);
  632. }
  633. }
  634. static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
  635. LPOVERLAPPED overlap)
  636. {
  637. struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
  638. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
  639. "bytes=%d", dst, (int) err, (int) bytes);
  640. if (err == 0 && bytes > 0)
  641. wpa_supplicant_global_iface_rx(dst, bytes);
  642. }
  643. static void wpa_supplicant_global_iface_receive(void *eloop_data,
  644. void *user_ctx)
  645. {
  646. struct wpa_global_dst *dst = eloop_data;
  647. struct ctrl_iface_global_priv *priv = dst->priv;
  648. DWORD bytes;
  649. wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
  650. ResetEvent(dst->overlap.hEvent);
  651. if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
  652. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
  653. (int) GetLastError());
  654. return;
  655. }
  656. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
  657. "connected");
  658. /* Open a new named pipe for the next client. */
  659. if (global_open_pipe(priv) < 0) {
  660. wpa_printf(MSG_DEBUG, "CTRL: global_open_pipe failed");
  661. return;
  662. }
  663. /* Use write completion function to start reading a command */
  664. global_iface_write_completed(0, 0, &dst->overlap);
  665. global_flush_broken_pipes(priv);
  666. }
  667. struct ctrl_iface_global_priv *
  668. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  669. {
  670. struct ctrl_iface_global_priv *priv;
  671. priv = os_zalloc(sizeof(*priv));
  672. if (priv == NULL)
  673. return NULL;
  674. priv->global = global;
  675. if (global_open_pipe(priv) < 0) {
  676. os_free(priv);
  677. return NULL;
  678. }
  679. return priv;
  680. }
  681. void
  682. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  683. {
  684. while (priv->ctrl_dst)
  685. global_close_pipe(priv->ctrl_dst);
  686. os_free(priv);
  687. }