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 *) &priv->attr.lpSecurityDescriptor,
418
wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
419
"security descriptor: %d",
420
sddl, (int) GetLastError());
425
priv->sec_attr_set = 1;
431
static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
432
const char *txt, size_t len)
434
struct wpa_supplicant *wpa_s = ctx;
435
if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
437
wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
441
struct ctrl_iface_priv *
442
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
444
struct ctrl_iface_priv *priv;
446
priv = os_zalloc(sizeof(*priv));
451
if (wpa_s->conf->ctrl_interface == NULL)
454
if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
459
if (ctrl_open_pipe(priv) < 0) {
464
wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
470
void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
472
while (priv->ctrl_dst)
473
ctrl_close_pipe(priv->ctrl_dst);
474
if (priv->sec_attr_set)
475
LocalFree(priv->attr.lpSecurityDescriptor);
480
static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
481
int level, const char *buf,
484
struct wpa_ctrl_dst *dst, *next;
491
dst = priv->ctrl_dst;
495
os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
497
llen = os_strlen(levelstr);
498
sbuf = os_malloc(llen + len);
502
os_memcpy(sbuf, levelstr, llen);
503
os_memcpy(sbuf + llen, buf, len);
508
if (dst->attached && level >= dst->debug_level) {
509
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
511
if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
513
wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
515
dst, (int) GetLastError());
517
if (dst->errors > 10)
518
ctrl_close_pipe(dst);
529
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
531
wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
532
priv->wpa_s->ifname);
533
if (priv->ctrl_dst == NULL)
535
WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
539
/* Global ctrl_iface */
541
struct ctrl_iface_global_priv;
543
struct wpa_global_dst {
544
/* Note: OVERLAPPED must be the first member of struct wpa_global_dst
547
struct wpa_global_dst *next, *prev;
548
struct ctrl_iface_global_priv *priv;
550
char req_buf[REQUEST_BUFSIZE];
555
struct ctrl_iface_global_priv {
556
struct wpa_global *global;
557
struct wpa_global_dst *ctrl_dst;
561
static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
563
struct wpa_global_dst *dst, *next;
565
dst = priv->ctrl_dst;
569
if (ctrl_broken_pipe(dst->pipe, dst->used)) {
570
wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
572
global_close_pipe(dst);
579
static int global_open_pipe(struct ctrl_iface_global_priv *priv)
581
struct wpa_global_dst *dst;
584
dst = os_zalloc(sizeof(*dst));
587
wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
590
dst->pipe = INVALID_HANDLE_VALUE;
592
dst->overlap.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
593
if (dst->overlap.hEvent == NULL) {
594
wpa_printf(MSG_ERROR, "CTRL: CreateEvent failed: %d",
595
(int) GetLastError());
599
eloop_register_event(dst->overlap.hEvent,
600
sizeof(dst->overlap.hEvent),
601
wpa_supplicant_global_iface_receive, dst, NULL);
603
/* TODO: add support for configuring access list for the pipe */
604
dst->pipe = CreateNamedPipe(NAMED_PIPE_PREFIX,
605
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
607
PIPE_READMODE_MESSAGE |
609
10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
611
if (dst->pipe == INVALID_HANDLE_VALUE) {
612
wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
613
(int) GetLastError());
617
if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
618
wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
619
(int) GetLastError());
620
CloseHandle(dst->pipe);
625
err = GetLastError();
627
case ERROR_IO_PENDING:
628
wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
631
case ERROR_PIPE_CONNECTED:
632
wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
634
if (SetEvent(dst->overlap.hEvent))
638
wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
640
CloseHandle(dst->pipe);
645
dst->next = priv->ctrl_dst;
647
dst->next->prev = dst;
648
priv->ctrl_dst = dst;
653
global_close_pipe(dst);
658
static void global_close_pipe(struct wpa_global_dst *dst)
660
wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
662
if (dst->overlap.hEvent) {
663
eloop_unregister_event(dst->overlap.hEvent,
664
sizeof(dst->overlap.hEvent));
665
CloseHandle(dst->overlap.hEvent);
668
if (dst->pipe != INVALID_HANDLE_VALUE) {
670
* Could use FlushFileBuffers() here to guarantee that all data
671
* gets delivered to the client, but that can block, so let's
672
* not do this for now.
673
* FlushFileBuffers(dst->pipe);
675
CloseHandle(dst->pipe);
679
dst->prev->next = dst->next;
681
dst->priv->ctrl_dst = dst->next;
683
dst->next->prev = dst->prev;
685
os_free(dst->rsp_buf);
690
static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
691
LPOVERLAPPED overlap)
693
struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
694
wpa_printf(MSG_DEBUG, "CTRL: Overlapped write completed: dst=%p "
695
"err=%d bytes=%d", dst, (int) err, (int) bytes);
697
global_close_pipe(dst);
701
os_free(dst->rsp_buf);
704
if (!ReadFileEx(dst->pipe, dst->req_buf, sizeof(dst->req_buf),
705
&dst->overlap, global_iface_read_completed)) {
706
wpa_printf(MSG_DEBUG, "CTRL: ReadFileEx failed: %d",
707
(int) GetLastError());
708
global_close_pipe(dst);
709
/* FIX: if this was the pipe waiting for new global
710
* connections, at this point there are no open global pipes..
711
* Should try to open a new pipe.. */
714
wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
718
static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
721
struct wpa_global *global = dst->priv->global;
722
char *reply = NULL, *send_buf;
723
size_t reply_len = 0, send_len;
724
char *buf = dst->req_buf;
727
if (len >= REQUEST_BUFSIZE)
728
len = REQUEST_BUFSIZE - 1;
731
reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
735
send_len = reply_len;
736
} else if (reply_len) {
740
os_free(dst->rsp_buf);
745
os_free(dst->rsp_buf);
746
dst->rsp_buf = os_malloc(send_len);
747
if (dst->rsp_buf == NULL) {
748
global_close_pipe(dst);
752
os_memcpy(dst->rsp_buf, send_buf, send_len);
755
if (!WriteFileEx(dst->pipe, dst->rsp_buf, send_len, &dst->overlap,
756
global_iface_write_completed)) {
757
wpa_printf(MSG_DEBUG, "CTRL: WriteFileEx failed: %d",
758
(int) GetLastError());
759
global_close_pipe(dst);
761
wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
767
static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
768
LPOVERLAPPED overlap)
770
struct wpa_global_dst *dst = (struct wpa_global_dst *) overlap;
771
wpa_printf(MSG_DEBUG, "CTRL: Overlapped read completed: dst=%p err=%d "
772
"bytes=%d", dst, (int) err, (int) bytes);
773
if (err == 0 && bytes > 0)
774
wpa_supplicant_global_iface_rx(dst, bytes);
778
static void wpa_supplicant_global_iface_receive(void *eloop_data,
781
struct wpa_global_dst *dst = eloop_data;
782
struct ctrl_iface_global_priv *priv = dst->priv;
785
wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
786
ResetEvent(dst->overlap.hEvent);
788
if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
789
wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
790
(int) GetLastError());
793
wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
796
/* Open a new named pipe for the next client. */
797
if (global_open_pipe(priv) < 0) {
798
wpa_printf(MSG_DEBUG, "CTRL: global_open_pipe failed");
802
/* Use write completion function to start reading a command */
803
global_iface_write_completed(0, 0, &dst->overlap);
805
global_flush_broken_pipes(priv);
809
struct ctrl_iface_global_priv *
810
wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
812
struct ctrl_iface_global_priv *priv;
814
priv = os_zalloc(sizeof(*priv));
817
priv->global = global;
819
if (global_open_pipe(priv) < 0) {
829
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
831
while (priv->ctrl_dst)
832
global_close_pipe(priv->ctrl_dst);