2 * WPA Supplicant / Windows Named Pipe -based control interface
3 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "wpa_supplicant_i.h"
22 #include "ctrl_iface.h"
25 #ifdef __MINGW32_VERSION
26 /* mingw-w32api v3.1 does not yet include sddl.h, so define needed parts here
28 #define SDDL_REVISION_1 1
29 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorA(
30 LPCSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
31 BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorW(
32 LPCWSTR, DWORD, PSECURITY_DESCRIPTOR *, PULONG);
34 #define ConvertStringSecurityDescriptorToSecurityDescriptor \
35 ConvertStringSecurityDescriptorToSecurityDescriptorW
37 #define ConvertStringSecurityDescriptorToSecurityDescriptor \
38 ConvertStringSecurityDescriptorToSecurityDescriptorA
40 #else /* __MINGW32_VERSION */
42 #define _WIN32_WINNT 0x0500
45 #endif /* __MINGW32_VERSION */
47 #ifndef WPA_SUPPLICANT_NAMED_PIPE
48 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
50 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
52 /* Per-interface ctrl_iface */
54 #define REQUEST_BUFSIZE 256
55 #define REPLY_BUFSIZE 4096
57 struct ctrl_iface_priv;
60 * struct wpa_ctrl_dst - Internal data structure of control interface clients
62 * This structure is used to store information about registered control
63 * interface monitors into struct wpa_supplicant. This data is private to
64 * ctrl_iface_named_pipe.c and should not be touched directly from other files.
67 /* Note: OVERLAPPED must be the first member of struct wpa_ctrl_dst */
69 struct wpa_ctrl_dst *next, *prev;
70 struct ctrl_iface_priv *priv;
75 char req_buf[REQUEST_BUFSIZE];
81 struct ctrl_iface_priv {
82 struct wpa_supplicant *wpa_s;
83 struct wpa_ctrl_dst *ctrl_dst;
84 SECURITY_ATTRIBUTES attr;
89 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
90 int level, const char *buf,
93 static void ctrl_close_pipe(struct wpa_ctrl_dst *dst);
94 static void wpa_supplicant_ctrl_iface_receive(void *, void *);
95 static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
96 LPOVERLAPPED overlap);
98 struct wpa_global_dst;
99 static void global_close_pipe(struct wpa_global_dst *dst);
100 static void wpa_supplicant_global_iface_receive(void *eloop_data,
102 static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
103 LPOVERLAPPED overlap);
106 static int ctrl_broken_pipe(HANDLE pipe, int used)
110 if (PeekNamedPipe(pipe, NULL, 0, NULL, NULL, NULL))
113 err = GetLastError();
114 if (err == ERROR_BROKEN_PIPE || (err == ERROR_BAD_PIPE && used))
120 static void ctrl_flush_broken_pipes(struct ctrl_iface_priv *priv)
122 struct wpa_ctrl_dst *dst, *next;
124 dst = priv->ctrl_dst;
128 if (ctrl_broken_pipe(dst->pipe, dst->used)) {
129 wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
131 ctrl_close_pipe(dst);
138 static int ctrl_open_pipe(struct ctrl_iface_priv *priv)
140 struct wpa_ctrl_dst *dst;
144 dst = os_zalloc(sizeof(*dst));
147 wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
150 dst->debug_level = MSG_INFO;
151 dst->pipe = INVALID_HANDLE_VALUE;
153 dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
154 if (dst->overlap.hEvent == NULL) {
155 wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
156 (int) GetLastError());
160 eloop_register_event(dst->overlap.hEvent,
161 sizeof(dst->overlap.hEvent),
162 wpa_supplicant_ctrl_iface_receive, dst, NULL);
165 _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
166 priv->wpa_s->ifname);
168 os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
169 priv->wpa_s->ifname);
172 /* TODO: add support for configuring access list for the pipe */
173 dst->pipe = CreateNamedPipe(name,
174 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
176 PIPE_READMODE_MESSAGE |
178 15, REPLY_BUFSIZE, REQUEST_BUFSIZE,
180 priv->sec_attr_set ? &priv->attr : NULL);
181 if (dst->pipe == INVALID_HANDLE_VALUE) {
182 wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
183 (int) GetLastError());
187 if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
188 wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
189 (int) GetLastError());
190 CloseHandle(dst->pipe);
195 err = GetLastError();
197 case ERROR_IO_PENDING:
198 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
201 case ERROR_PIPE_CONNECTED:
202 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
204 if (SetEvent(dst->overlap.hEvent))
208 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
210 CloseHandle(dst->pipe);
215 dst->next = priv->ctrl_dst;
217 dst->next->prev = dst;
218 priv->ctrl_dst = dst;
223 ctrl_close_pipe(dst);
228 static void ctrl_close_pipe(struct wpa_ctrl_dst *dst)
230 wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
232 if (dst->overlap.hEvent) {
233 eloop_unregister_event(dst->overlap.hEvent,
234 sizeof(dst->overlap.hEvent));
235 CloseHandle(dst->overlap.hEvent);
238 if (dst->pipe != INVALID_HANDLE_VALUE) {
240 * Could use FlushFileBuffers() here to guarantee that all data
241 * gets delivered to the client, but that can block, so let's
242 * not do this for now.
243 * FlushFileBuffers(dst->pipe);
245 CloseHandle(dst->pipe);
249 dst->prev->next = dst->next;
251 dst->priv->ctrl_dst = dst->next;
253 dst->next->prev = dst->prev;
255 os_free(dst->rsp_buf);
260 static VOID WINAPI ctrl_iface_write_completed(DWORD err, DWORD bytes,
261 LPOVERLAPPED overlap)
263 struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
264 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
265 "err=%d bytes=%d", dst, (int) err, (int) bytes);
267 ctrl_close_pipe(dst);
271 os_free(dst->rsp_buf);
274 if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
275 &dst->overlap, ctrl_iface_read_completed)) {
276 wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
277 (int) GetLastError());
278 ctrl_close_pipe(dst);
281 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
285 static void wpa_supplicant_ctrl_iface_rx(struct wpa_ctrl_dst *dst, size_t len)
287 struct wpa_supplicant *wpa_s = dst->priv->wpa_s;
288 char *reply = NULL, *send_buf;
289 size_t reply_len = 0, send_len;
290 int new_attached = 0;
291 char *buf = dst->req_buf;
294 if (len >= REQUEST_BUFSIZE)
295 len = REQUEST_BUFSIZE - 1;
298 if (os_strcmp(buf, "ATTACH") == 0) {
300 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached");
303 } else if (os_strcmp(buf, "DETACH") == 0) {
305 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached");
307 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
308 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", buf + 6);
309 dst->debug_level = atoi(buf + 6);
312 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
318 send_len = reply_len;
319 } else if (reply_len == 2) {
327 os_free(dst->rsp_buf);
328 dst->rsp_buf = os_malloc(send_len);
329 if (dst->rsp_buf == NULL) {
330 ctrl_close_pipe(dst);
334 os_memcpy(dst->rsp_buf, send_buf, send_len);
337 if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
338 ctrl_iface_write_completed)) {
339 wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
340 (int) GetLastError());
341 ctrl_close_pipe(dst);
343 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
348 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
352 static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
353 LPOVERLAPPED overlap)
355 struct wpa_ctrl_dst *dst = (struct wpa_ctrl_dst *) overlap;
356 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
357 "bytes=%d", dst, (int) err, (int) bytes);
358 if (err == 0 && bytes > 0)
359 wpa_supplicant_ctrl_iface_rx(dst, bytes);
363 static void wpa_supplicant_ctrl_iface_receive(void *eloop_data, void *user_ctx)
365 struct wpa_ctrl_dst *dst = eloop_data;
366 struct ctrl_iface_priv *priv = dst->priv;
369 wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_ctrl_iface_receive");
370 ResetEvent(dst->overlap.hEvent);
372 if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
373 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
374 (int) GetLastError());
377 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
380 /* Open a new named pipe for the next client. */
381 ctrl_open_pipe(priv);
383 /* Use write completion function to start reading a command */
384 ctrl_iface_write_completed(0, 0, &dst->overlap);
386 ctrl_flush_broken_pipes(priv);
390 static int ctrl_iface_parse(struct ctrl_iface_priv *priv, const char *params)
392 const char *sddl = NULL;
395 if (os_strncmp(params, "SDDL=", 5) == 0)
398 sddl = os_strstr(params, " SDDL=");
406 wpa_printf(MSG_DEBUG, "CTRL: SDDL='%s'", sddl);
407 os_memset(&priv->attr, 0, sizeof(priv->attr));
408 priv->attr.nLength = sizeof(priv->attr);
409 priv->attr.bInheritHandle = FALSE;
410 t_sddl = wpa_strdup_tchar(sddl);
413 if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
414 t_sddl, SDDL_REVISION_1,
415 (PSECURITY_DESCRIPTOR *) (void *)
416 &priv->attr.lpSecurityDescriptor,
419 wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
420 "security descriptor: %d",
421 sddl, (int) GetLastError());
426 priv->sec_attr_set = 1;
432 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
433 const char *txt, size_t len)
435 struct wpa_supplicant *wpa_s = ctx;
436 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
438 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
442 struct ctrl_iface_priv *
443 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
445 struct ctrl_iface_priv *priv;
447 priv = os_zalloc(sizeof(*priv));
452 if (wpa_s->conf->ctrl_interface == NULL)
455 if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
460 if (ctrl_open_pipe(priv) < 0) {
465 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
471 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
473 while (priv->ctrl_dst)
474 ctrl_close_pipe(priv->ctrl_dst);
475 if (priv->sec_attr_set)
476 LocalFree(priv->attr.lpSecurityDescriptor);
481 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
482 int level, const char *buf,
485 struct wpa_ctrl_dst *dst, *next;
492 dst = priv->ctrl_dst;
496 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
498 llen = os_strlen(levelstr);
499 sbuf = os_malloc(llen + len);
503 os_memcpy(sbuf, levelstr, llen);
504 os_memcpy(sbuf + llen, buf, len);
509 if (dst->attached && level >= dst->debug_level) {
510 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
512 if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
514 wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
516 dst, (int) GetLastError());
518 if (dst->errors > 10)
519 ctrl_close_pipe(dst);
530 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
532 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
533 priv->wpa_s->ifname);
534 if (priv->ctrl_dst == NULL)
536 WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
540 /* Global ctrl_iface */
542 struct ctrl_iface_global_priv;
544 struct wpa_global_dst {
545 /* Note: OVERLAPPED must be the first member of struct wpa_global_dst
548 struct wpa_global_dst *next, *prev;
549 struct ctrl_iface_global_priv *priv;
551 char req_buf[REQUEST_BUFSIZE];
556 struct ctrl_iface_global_priv {
557 struct wpa_global *global;
558 struct wpa_global_dst *ctrl_dst;
562 static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
564 struct wpa_global_dst *dst, *next;
566 dst = priv->ctrl_dst;
570 if (ctrl_broken_pipe(dst->pipe, dst->used)) {
571 wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
573 global_close_pipe(dst);
580 static int global_open_pipe(struct ctrl_iface_global_priv *priv)
582 struct wpa_global_dst *dst;
585 dst = os_zalloc(sizeof(*dst));
588 wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
591 dst->pipe = INVALID_HANDLE_VALUE;
593 dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
594 if (dst->overlap.hEvent == NULL) {
595 wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
596 (int) GetLastError());
600 eloop_register_event(dst->overlap.hEvent,
601 sizeof(dst->overlap.hEvent),
602 wpa_supplicant_global_iface_receive, dst, NULL);
604 /* TODO: add support for configuring access list for the pipe */
605 dst->pipe = CreateNamedPipe(NAMED_PIPE_PREFIX,
606 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
608 PIPE_READMODE_MESSAGE |
610 10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
612 if (dst->pipe == INVALID_HANDLE_VALUE) {
613 wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
614 (int) GetLastError());
618 if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
619 wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
620 (int) GetLastError());
621 CloseHandle(dst->pipe);
626 err = GetLastError();
628 case ERROR_IO_PENDING:
629 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
632 case ERROR_PIPE_CONNECTED:
633 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
635 if (SetEvent(dst->overlap.hEvent))
639 wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
641 CloseHandle(dst->pipe);
646 dst->next = priv->ctrl_dst;
648 dst->next->prev = dst;
649 priv->ctrl_dst = dst;
654 global_close_pipe(dst);
659 static void global_close_pipe(struct wpa_global_dst *dst)
661 wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
663 if (dst->overlap.hEvent) {
664 eloop_unregister_event(dst->overlap.hEvent,
665 sizeof(dst->overlap.hEvent));
666 CloseHandle(dst->overlap.hEvent);
669 if (dst->pipe != INVALID_HANDLE_VALUE) {
671 * Could use FlushFileBuffers() here to guarantee that all data
672 * gets delivered to the client, but that can block, so let's
673 * not do this for now.
674 * FlushFileBuffers(dst->pipe);
676 CloseHandle(dst->pipe);
680 dst->prev->next = dst->next;
682 dst->priv->ctrl_dst = dst->next;
684 dst->next->prev = dst->prev;
686 os_free(dst->rsp_buf);
691 static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
692 LPOVERLAPPED overlap)
694 struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
695 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
696 "err=%d bytes=%d", dst, (int) err, (int) bytes);
698 global_close_pipe(dst);
702 os_free(dst->rsp_buf);
705 if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
706 &dst->overlap, global_iface_read_completed)) {
707 wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
708 (int) GetLastError());
709 global_close_pipe(dst);
710 /* FIX: if this was the pipe waiting for new global
711 * connections, at this point there are no open global pipes..
712 * Should try to open a new pipe.. */
715 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
719 static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
722 struct wpa_global *global = dst->priv->global;
723 char *reply = NULL, *send_buf;
724 size_t reply_len = 0, send_len;
725 char *buf = dst->req_buf;
728 if (len >= REQUEST_BUFSIZE)
729 len = REQUEST_BUFSIZE - 1;
732 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
736 send_len = reply_len;
737 } else if (reply_len) {
741 os_free(dst->rsp_buf);
746 os_free(dst->rsp_buf);
747 dst->rsp_buf = os_malloc(send_len);
748 if (dst->rsp_buf == NULL) {
749 global_close_pipe(dst);
753 os_memcpy(dst->rsp_buf, send_buf, send_len);
756 if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
757 global_iface_write_completed)) {
758 wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
759 (int) GetLastError());
760 global_close_pipe(dst);
762 wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
768 static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
769 LPOVERLAPPED overlap)
771 struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
772 wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
773 "bytes=%d", dst, (int) err, (int) bytes);
774 if (err == 0 && bytes > 0)
775 wpa_supplicant_global_iface_rx(dst, bytes);
779 static void wpa_supplicant_global_iface_receive(void *eloop_data,
782 struct wpa_global_dst *dst = eloop_data;
783 struct ctrl_iface_global_priv *priv = dst->priv;
786 wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
787 ResetEvent(dst->overlap.hEvent);
789 if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
790 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
791 (int) GetLastError());
794 wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
797 /* Open a new named pipe for the next client. */
798 if (global_open_pipe(priv) < 0) {
799 wpa_printf(MSG_DEBUG, "CTRL: global_open_pipe failed");
803 /* Use write completion function to start reading a command */
804 global_iface_write_completed(0, 0, &dst->overlap);
806 global_flush_broken_pipes(priv);
810 struct ctrl_iface_global_priv *
811 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
813 struct ctrl_iface_global_priv *priv;
815 priv = os_zalloc(sizeof(*priv));
818 priv->global = global;
820 if (global_open_pipe(priv) < 0) {
830 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
832 while (priv->ctrl_dst)
833 global_close_pipe(priv->ctrl_dst);