Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ctrl_iface_named_pipe.c 20KB

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