ctrl_iface_named_pipe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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_memdup(send_buf, send_len);
  272. if (dst->rsp_buf == NULL) {
  273. ctrl_close_pipe(dst);
  274. os_free(reply);
  275. return;
  276. }
  277. os_free(reply);
  278. if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
  279. ctrl_iface_write_completed)) {
  280. wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
  281. (int) GetLastError());
  282. ctrl_close_pipe(dst);
  283. } else {
  284. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
  285. dst);
  286. }
  287. if (new_attached)
  288. eapol_sm_notify_ctrl_attached(wpa_s->eapol);
  289. }
  290. static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
  291. LPOVERLAPPED overlap)
  292. {
  293. struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
  294. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
  295. "bytes=%d", dst, (int) err, (int) bytes);
  296. if (err == 0 && bytes > 0)
  297. wpa_supplicant_ctrl_iface_rx(dst, bytes);
  298. }
  299. static void wpa_supplicant_ctrl_iface_receive(void *eloop_data, void *user_ctx)
  300. {
  301. struct wpa_ctrl_dst *dst = eloop_data;
  302. struct ctrl_iface_priv *priv = dst->priv;
  303. DWORD bytes;
  304. wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_ctrl_iface_receive");
  305. ResetEvent(dst->overlap.hEvent);
  306. if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
  307. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
  308. (int) GetLastError());
  309. return;
  310. }
  311. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
  312. "connected");
  313. /* Open a new named pipe for the next client. */
  314. ctrl_open_pipe(priv);
  315. /* Use write completion function to start reading a command */
  316. ctrl_iface_write_completed(0, 0, &dst->overlap);
  317. ctrl_flush_broken_pipes(priv);
  318. }
  319. static int ctrl_iface_parse(struct ctrl_iface_priv *priv, const char *params)
  320. {
  321. const char *sddl = NULL;
  322. TCHAR *t_sddl;
  323. if (os_strncmp(params, "SDDL=", 5) == 0)
  324. sddl = params + 5;
  325. if (!sddl) {
  326. sddl = os_strstr(params, " SDDL=");
  327. if (sddl)
  328. sddl += 6;
  329. }
  330. if (!sddl)
  331. return 0;
  332. wpa_printf(MSG_DEBUG, "CTRL: SDDL='%s'", sddl);
  333. os_memset(&priv->attr, 0, sizeof(priv->attr));
  334. priv->attr.nLength = sizeof(priv->attr);
  335. priv->attr.bInheritHandle = FALSE;
  336. t_sddl = wpa_strdup_tchar(sddl);
  337. if (t_sddl == NULL)
  338. return -1;
  339. if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
  340. t_sddl, SDDL_REVISION_1,
  341. (PSECURITY_DESCRIPTOR *) (void *)
  342. &priv->attr.lpSecurityDescriptor,
  343. NULL)) {
  344. os_free(t_sddl);
  345. wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
  346. "security descriptor: %d",
  347. sddl, (int) GetLastError());
  348. return -1;
  349. }
  350. os_free(t_sddl);
  351. priv->sec_attr_set = 1;
  352. return 0;
  353. }
  354. static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
  355. enum wpa_msg_type type,
  356. const char *txt, size_t len)
  357. {
  358. struct wpa_supplicant *wpa_s = ctx;
  359. if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
  360. return;
  361. wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
  362. }
  363. struct ctrl_iface_priv *
  364. wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
  365. {
  366. struct ctrl_iface_priv *priv;
  367. priv = os_zalloc(sizeof(*priv));
  368. if (priv == NULL)
  369. return NULL;
  370. priv->wpa_s = wpa_s;
  371. if (wpa_s->conf->ctrl_interface == NULL)
  372. return priv;
  373. if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
  374. os_free(priv);
  375. return NULL;
  376. }
  377. if (ctrl_open_pipe(priv) < 0) {
  378. os_free(priv);
  379. return NULL;
  380. }
  381. wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
  382. return priv;
  383. }
  384. void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
  385. {
  386. while (priv->ctrl_dst)
  387. ctrl_close_pipe(priv->ctrl_dst);
  388. if (priv->sec_attr_set)
  389. LocalFree(priv->attr.lpSecurityDescriptor);
  390. os_free(priv);
  391. }
  392. static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
  393. int level, const char *buf,
  394. size_t len)
  395. {
  396. struct wpa_ctrl_dst *dst, *next;
  397. char levelstr[10];
  398. int idx;
  399. char *sbuf;
  400. int llen;
  401. DWORD written;
  402. dst = priv->ctrl_dst;
  403. if (dst == NULL)
  404. return;
  405. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  406. llen = os_strlen(levelstr);
  407. sbuf = os_malloc(llen + len);
  408. if (sbuf == NULL)
  409. return;
  410. os_memcpy(sbuf, levelstr, llen);
  411. os_memcpy(sbuf + llen, buf, len);
  412. idx = 0;
  413. while (dst) {
  414. next = dst->next;
  415. if (dst->attached && level >= dst->debug_level) {
  416. wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
  417. dst);
  418. if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
  419. NULL)) {
  420. wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
  421. "%p failed: %d",
  422. dst, (int) GetLastError());
  423. dst->errors++;
  424. if (dst->errors > 10)
  425. ctrl_close_pipe(dst);
  426. } else
  427. dst->errors = 0;
  428. }
  429. idx++;
  430. dst = next;
  431. }
  432. os_free(sbuf);
  433. }
  434. void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
  435. {
  436. wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
  437. priv->wpa_s->ifname);
  438. if (priv->ctrl_dst == NULL)
  439. return;
  440. WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
  441. }
  442. /* Global ctrl_iface */
  443. struct ctrl_iface_global_priv;
  444. struct wpa_global_dst {
  445. /* Note: OVERLAPPED must be the first member of struct wpa_global_dst
  446. */
  447. OVERLAPPED overlap;
  448. struct wpa_global_dst *next, *prev;
  449. struct ctrl_iface_global_priv *priv;
  450. HANDLE pipe;
  451. char req_buf[REQUEST_BUFSIZE];
  452. char *rsp_buf;
  453. int used;
  454. };
  455. struct ctrl_iface_global_priv {
  456. struct wpa_global *global;
  457. struct wpa_global_dst *ctrl_dst;
  458. };
  459. static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
  460. {
  461. struct wpa_global_dst *dst, *next;
  462. dst = priv->ctrl_dst;
  463. while (dst) {
  464. next = dst->next;
  465. if (ctrl_broken_pipe(dst->pipe, dst->used)) {
  466. wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
  467. dst);
  468. global_close_pipe(dst);
  469. }
  470. dst = next;
  471. }
  472. }
  473. static int global_open_pipe(struct ctrl_iface_global_priv *priv)
  474. {
  475. struct wpa_global_dst *dst;
  476. DWORD err;
  477. dst = os_zalloc(sizeof(*dst));
  478. if (dst == NULL)
  479. return -1;
  480. wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
  481. dst->priv = priv;
  482. dst->pipe = INVALID_HANDLE_VALUE;
  483. dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
  484. if (dst->overlap.hEvent == NULL) {
  485. wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
  486. (int) GetLastError());
  487. goto fail;
  488. }
  489. eloop_register_event(dst->overlap.hEvent,
  490. sizeof(dst->overlap.hEvent),
  491. wpa_supplicant_global_iface_receive, dst, NULL);
  492. /* TODO: add support for configuring access list for the pipe */
  493. dst->pipe = CreateNamedPipe(NAMED_PIPE_PREFIX,
  494. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  495. PIPE_TYPE_MESSAGE |
  496. PIPE_READMODE_MESSAGE |
  497. PIPE_WAIT,
  498. 10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
  499. 1000, NULL);
  500. if (dst->pipe == INVALID_HANDLE_VALUE) {
  501. wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
  502. (int) GetLastError());
  503. goto fail;
  504. }
  505. if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
  506. wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
  507. (int) GetLastError());
  508. CloseHandle(dst->pipe);
  509. os_free(dst);
  510. return -1;
  511. }
  512. err = GetLastError();
  513. switch (err) {
  514. case ERROR_IO_PENDING:
  515. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
  516. "progress");
  517. break;
  518. case ERROR_PIPE_CONNECTED:
  519. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
  520. "connected");
  521. if (SetEvent(dst->overlap.hEvent))
  522. break;
  523. /* fall through */
  524. default:
  525. wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
  526. (int) err);
  527. CloseHandle(dst->pipe);
  528. os_free(dst);
  529. return -1;
  530. }
  531. dst->next = priv->ctrl_dst;
  532. if (dst->next)
  533. dst->next->prev = dst;
  534. priv->ctrl_dst = dst;
  535. return 0;
  536. fail:
  537. global_close_pipe(dst);
  538. return -1;
  539. }
  540. static void global_close_pipe(struct wpa_global_dst *dst)
  541. {
  542. wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
  543. if (dst->overlap.hEvent) {
  544. eloop_unregister_event(dst->overlap.hEvent,
  545. sizeof(dst->overlap.hEvent));
  546. CloseHandle(dst->overlap.hEvent);
  547. }
  548. if (dst->pipe != INVALID_HANDLE_VALUE) {
  549. /*
  550. * Could use FlushFileBuffers() here to guarantee that all data
  551. * gets delivered to the client, but that can block, so let's
  552. * not do this for now.
  553. * FlushFileBuffers(dst->pipe);
  554. */
  555. CloseHandle(dst->pipe);
  556. }
  557. if (dst->prev)
  558. dst->prev->next = dst->next;
  559. else
  560. dst->priv->ctrl_dst = dst->next;
  561. if (dst->next)
  562. dst->next->prev = dst->prev;
  563. os_free(dst->rsp_buf);
  564. os_free(dst);
  565. }
  566. static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
  567. LPOVERLAPPED overlap)
  568. {
  569. struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
  570. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
  571. "err=%d bytes=%d", dst, (int) err, (int) bytes);
  572. if (err) {
  573. global_close_pipe(dst);
  574. return;
  575. }
  576. os_free(dst->rsp_buf);
  577. dst->rsp_buf = NULL;
  578. if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
  579. &dst->overlap, global_iface_read_completed)) {
  580. wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
  581. (int) GetLastError());
  582. global_close_pipe(dst);
  583. /* FIX: if this was the pipe waiting for new global
  584. * connections, at this point there are no open global pipes..
  585. * Should try to open a new pipe.. */
  586. return;
  587. }
  588. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
  589. }
  590. static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
  591. size_t len)
  592. {
  593. struct wpa_global *global = dst->priv->global;
  594. char *reply = NULL, *send_buf;
  595. size_t reply_len = 0, send_len;
  596. char *buf = dst->req_buf;
  597. dst->used = 1;
  598. if (len >= REQUEST_BUFSIZE)
  599. len = REQUEST_BUFSIZE - 1;
  600. buf[len] = '\0';
  601. reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
  602. &reply_len);
  603. if (reply) {
  604. send_buf = reply;
  605. send_len = reply_len;
  606. } else if (reply_len) {
  607. send_buf = "FAIL\n";
  608. send_len = 5;
  609. } else {
  610. os_free(dst->rsp_buf);
  611. dst->rsp_buf = NULL;
  612. return;
  613. }
  614. os_free(dst->rsp_buf);
  615. dst->rsp_buf = os_memdup(send_buf, send_len);
  616. if (dst->rsp_buf == NULL) {
  617. global_close_pipe(dst);
  618. os_free(reply);
  619. return;
  620. }
  621. os_free(reply);
  622. if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
  623. global_iface_write_completed)) {
  624. wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
  625. (int) GetLastError());
  626. global_close_pipe(dst);
  627. } else {
  628. wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
  629. dst);
  630. }
  631. }
  632. static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
  633. LPOVERLAPPED overlap)
  634. {
  635. struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
  636. wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
  637. "bytes=%d", dst, (int) err, (int) bytes);
  638. if (err == 0 && bytes > 0)
  639. wpa_supplicant_global_iface_rx(dst, bytes);
  640. }
  641. static void wpa_supplicant_global_iface_receive(void *eloop_data,
  642. void *user_ctx)
  643. {
  644. struct wpa_global_dst *dst = eloop_data;
  645. struct ctrl_iface_global_priv *priv = dst->priv;
  646. DWORD bytes;
  647. wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
  648. ResetEvent(dst->overlap.hEvent);
  649. if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
  650. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
  651. (int) GetLastError());
  652. return;
  653. }
  654. wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
  655. "connected");
  656. /* Open a new named pipe for the next client. */
  657. if (global_open_pipe(priv) < 0) {
  658. wpa_printf(MSG_DEBUG, "CTRL: global_open_pipe failed");
  659. return;
  660. }
  661. /* Use write completion function to start reading a command */
  662. global_iface_write_completed(0, 0, &dst->overlap);
  663. global_flush_broken_pipes(priv);
  664. }
  665. struct ctrl_iface_global_priv *
  666. wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
  667. {
  668. struct ctrl_iface_global_priv *priv;
  669. priv = os_zalloc(sizeof(*priv));
  670. if (priv == NULL)
  671. return NULL;
  672. priv->global = global;
  673. if (global_open_pipe(priv) < 0) {
  674. os_free(priv);
  675. return NULL;
  676. }
  677. return priv;
  678. }
  679. void
  680. wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
  681. {
  682. while (priv->ctrl_dst)
  683. global_close_pipe(priv->ctrl_dst);
  684. os_free(priv);
  685. }