ctrl_iface_named_pipe.c 20 KB

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