~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to ctrl_iface_named_pipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
15
#include "includes.h"
 
16
 
 
17
#include "common.h"
 
18
#include "eloop.h"
 
19
#include "config.h"
 
20
#include "eapol_sm.h"
 
21
#include "wpa_supplicant_i.h"
 
22
#include "ctrl_iface.h"
 
23
#include "wpa_ctrl.h"
 
24
 
 
25
#ifdef __MINGW32_VERSION
 
26
/* mingw-w32api v3.1 does not yet include sddl.h, so define needed parts here
 
27
 */
 
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);
 
33
#ifdef UNICODE
 
34
#define ConvertStringSecurityDescriptorToSecurityDescriptor \
 
35
ConvertStringSecurityDescriptorToSecurityDescriptorW
 
36
#else
 
37
#define ConvertStringSecurityDescriptorToSecurityDescriptor \
 
38
ConvertStringSecurityDescriptorToSecurityDescriptorA
 
39
#endif
 
40
#else /* __MINGW32_VERSION */
 
41
#ifndef _WIN32_WINNT
 
42
#define _WIN32_WINNT 0x0500
 
43
#endif
 
44
#include <sddl.h>
 
45
#endif /* __MINGW32_VERSION */
 
46
 
 
47
#ifndef WPA_SUPPLICANT_NAMED_PIPE
 
48
#define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
 
49
#endif
 
50
#define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
 
51
 
 
52
/* Per-interface ctrl_iface */
 
53
 
 
54
#define REQUEST_BUFSIZE 256
 
55
#define REPLY_BUFSIZE 4096
 
56
 
 
57
struct ctrl_iface_priv;
 
58
 
 
59
/**
 
60
 * struct wpa_ctrl_dst - Internal data structure of control interface clients
 
61
 *
 
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.
 
65
 */
 
66
struct wpa_ctrl_dst {
 
67
        /* Note: OVERLAPPED must be the first member of struct wpa_ctrl_dst */
 
68
        OVERLAPPED overlap;
 
69
        struct wpa_ctrl_dst *next, *prev;
 
70
        struct ctrl_iface_priv *priv;
 
71
        HANDLE pipe;
 
72
        int attached;
 
73
        int debug_level;
 
74
        int errors;
 
75
        char req_buf[REQUEST_BUFSIZE];
 
76
        char *rsp_buf;
 
77
        int used;
 
78
};
 
79
 
 
80
 
 
81
struct ctrl_iface_priv {
 
82
        struct wpa_supplicant *wpa_s;
 
83
        struct wpa_ctrl_dst *ctrl_dst;
 
84
        SECURITY_ATTRIBUTES attr;
 
85
        int sec_attr_set;
 
86
};
 
87
 
 
88
 
 
89
static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 
90
                                           int level, const char *buf,
 
91
                                           size_t len);
 
92
 
 
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);
 
97
 
 
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,
 
101
                                                void *user_ctx);
 
102
static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
 
103
                                               LPOVERLAPPED overlap);
 
104
 
 
105
 
 
106
static int ctrl_broken_pipe(HANDLE pipe, int used)
 
107
{
 
108
        DWORD err;
 
109
 
 
110
        if (PeekNamedPipe(pipe, NULL, 0, NULL, NULL, NULL))
 
111
                return 0;
 
112
 
 
113
        err = GetLastError();
 
114
        if (err == ERROR_BROKEN_PIPE || (err == ERROR_BAD_PIPE && used))
 
115
                return 1;
 
116
        return 0;
 
117
}
 
118
 
 
119
 
 
120
static void ctrl_flush_broken_pipes(struct ctrl_iface_priv *priv)
 
121
{
 
122
        struct wpa_ctrl_dst *dst, *next;
 
123
 
 
124
        dst = priv->ctrl_dst;
 
125
 
 
126
        while (dst) {
 
127
                next = dst->next;
 
128
                if (ctrl_broken_pipe(dst->pipe, dst->used)) {
 
129
                        wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
 
130
                                   dst);
 
131
                        ctrl_close_pipe(dst);
 
132
                }
 
133
                dst = next;
 
134
        }
 
135
}
 
136
 
 
137
 
 
138
static int ctrl_open_pipe(struct ctrl_iface_priv *priv)
 
139
{
 
140
        struct wpa_ctrl_dst *dst;
 
141
        DWORD err;
 
142
        TCHAR name[256];
 
143
 
 
144
        dst = os_zalloc(sizeof(*dst));
 
145
        if (dst == NULL)
 
146
                return -1;
 
147
        wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
 
148
 
 
149
        dst->priv = priv;
 
150
        dst->debug_level = MSG_INFO;
 
151
        dst->pipe = INVALID_HANDLE_VALUE;
 
152
 
 
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());
 
157
                goto fail;
 
158
        }
 
159
 
 
160
        eloop_register_event(dst->overlap.hEvent,
 
161
                             sizeof(dst->overlap.hEvent),
 
162
                             wpa_supplicant_ctrl_iface_receive, dst, NULL);
 
163
 
 
164
#ifdef UNICODE
 
165
        _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
 
166
                   priv->wpa_s->ifname);
 
167
#else /* UNICODE */
 
168
        os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
 
169
                    priv->wpa_s->ifname);
 
170
#endif /* UNICODE */
 
171
 
 
172
        /* TODO: add support for configuring access list for the pipe */
 
173
        dst->pipe = CreateNamedPipe(name,
 
174
                                    PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
 
175
                                    PIPE_TYPE_MESSAGE |
 
176
                                    PIPE_READMODE_MESSAGE |
 
177
                                    PIPE_WAIT,
 
178
                                    15, REPLY_BUFSIZE, REQUEST_BUFSIZE,
 
179
                                    1000,
 
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());
 
184
                goto fail;
 
185
        }
 
186
 
 
187
        if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
 
188
                wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
 
189
                           (int) GetLastError());
 
190
                CloseHandle(dst->pipe);
 
191
                os_free(dst);
 
192
                return -1;
 
193
        }
 
194
 
 
195
        err = GetLastError();
 
196
        switch (err) {
 
197
        case ERROR_IO_PENDING:
 
198
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
 
199
                           "progress");
 
200
                break;
 
201
        case ERROR_PIPE_CONNECTED:
 
202
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
 
203
                           "connected");
 
204
                if (SetEvent(dst->overlap.hEvent))
 
205
                        break;
 
206
                /* fall through */
 
207
        default:
 
208
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
 
209
                           (int) err);
 
210
                CloseHandle(dst->pipe);
 
211
                os_free(dst);
 
212
                return -1;
 
213
        }
 
214
 
 
215
        dst->next = priv->ctrl_dst;
 
216
        if (dst->next)
 
217
                dst->next->prev = dst;
 
218
        priv->ctrl_dst = dst;
 
219
 
 
220
        return 0;
 
221
 
 
222
fail:
 
223
        ctrl_close_pipe(dst);
 
224
        return -1;
 
225
}
 
226
 
 
227
 
 
228
static void ctrl_close_pipe(struct wpa_ctrl_dst *dst)
 
229
{
 
230
        wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
 
231
 
 
232
        if (dst->overlap.hEvent) {
 
233
                eloop_unregister_event(dst->overlap.hEvent,
 
234
                                       sizeof(dst->overlap.hEvent));
 
235
                CloseHandle(dst->overlap.hEvent);
 
236
        }
 
237
 
 
238
        if (dst->pipe != INVALID_HANDLE_VALUE) {
 
239
                /*
 
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);
 
244
                 */
 
245
                CloseHandle(dst->pipe);
 
246
        }
 
247
 
 
248
        if (dst->prev)
 
249
                dst->prev->next = dst->next;
 
250
        else
 
251
                dst->priv->ctrl_dst = dst->next;
 
252
        if (dst->next)
 
253
                dst->next->prev = dst->prev;
 
254
 
 
255
        os_free(dst->rsp_buf);
 
256
        os_free(dst);
 
257
}
 
258
 
 
259
 
 
260
static VOID WINAPI ctrl_iface_write_completed(DWORD err, DWORD bytes,
 
261
                                              LPOVERLAPPED overlap)
 
262
{
 
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);
 
266
        if (err) {
 
267
                ctrl_close_pipe(dst);
 
268
                return;
 
269
        }
 
270
 
 
271
        os_free(dst->rsp_buf);
 
272
        dst->rsp_buf = NULL;
 
273
 
 
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);
 
279
                return;
 
280
        }
 
281
        wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
 
282
}
 
283
 
 
284
 
 
285
static void wpa_supplicant_ctrl_iface_rx(struct wpa_ctrl_dst *dst, size_t len)
 
286
{
 
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;
 
292
 
 
293
        dst->used = 1;
 
294
        if (len >= REQUEST_BUFSIZE)
 
295
                len = REQUEST_BUFSIZE - 1;
 
296
        buf[len] = '\0';
 
297
 
 
298
        if (os_strcmp(buf, "ATTACH") == 0) {
 
299
                dst->attached = 1;
 
300
                wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached");
 
301
                new_attached = 1;
 
302
                reply_len = 2;
 
303
        } else if (os_strcmp(buf, "DETACH") == 0) {
 
304
                dst->attached = 0;
 
305
                wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached");
 
306
                reply_len = 2;
 
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);
 
310
                reply_len = 2;
 
311
        } else {
 
312
                reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
 
313
                                                          &reply_len);
 
314
        }
 
315
 
 
316
        if (reply) {
 
317
                send_buf = reply;
 
318
                send_len = reply_len;
 
319
        } else if (reply_len == 2) {
 
320
                send_buf = "OK\n";
 
321
                send_len = 3;
 
322
        } else {
 
323
                send_buf = "FAIL\n";
 
324
                send_len = 5;
 
325
        }
 
326
 
 
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);
 
331
                os_free(reply);
 
332
                return;
 
333
        }
 
334
        os_memcpy(dst->rsp_buf, send_buf, send_len);
 
335
        os_free(reply);
 
336
 
 
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);
 
342
        } else {
 
343
                wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
 
344
                           dst);
 
345
        }
 
346
 
 
347
        if (new_attached)
 
348
                eapol_sm_notify_ctrl_attached(wpa_s->eapol);
 
349
}
 
350
 
 
351
 
 
352
static VOID WINAPI ctrl_iface_read_completed(DWORD err, DWORD bytes,
 
353
                                             LPOVERLAPPED overlap)
 
354
{
 
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);
 
360
}
 
361
 
 
362
 
 
363
static void wpa_supplicant_ctrl_iface_receive(void *eloop_data, void *user_ctx)
 
364
{
 
365
        struct wpa_ctrl_dst *dst = eloop_data;
 
366
        struct ctrl_iface_priv *priv = dst->priv;
 
367
        DWORD bytes;
 
368
 
 
369
        wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_ctrl_iface_receive");
 
370
        ResetEvent(dst->overlap.hEvent);
 
371
 
 
372
        if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
 
373
                wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
 
374
                           (int) GetLastError());
 
375
                return;
 
376
        }
 
377
        wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
 
378
                   "connected");
 
379
 
 
380
        /* Open a new named pipe for the next client. */
 
381
        ctrl_open_pipe(priv);
 
382
 
 
383
        /* Use write completion function to start reading a command */
 
384
        ctrl_iface_write_completed(0, 0, &dst->overlap);
 
385
 
 
386
        ctrl_flush_broken_pipes(priv);
 
387
}
 
388
 
 
389
 
 
390
static int ctrl_iface_parse(struct ctrl_iface_priv *priv, const char *params)
 
391
{
 
392
        const char *sddl = NULL;
 
393
        TCHAR *t_sddl;
 
394
 
 
395
        if (os_strncmp(params, "SDDL=", 5) == 0)
 
396
                sddl = params + 5;
 
397
        if (!sddl) {
 
398
                sddl = os_strstr(params, " SDDL=");
 
399
                if (sddl)
 
400
                        sddl += 6;
 
401
        }
 
402
 
 
403
        if (!sddl)
 
404
                return 0;
 
405
 
 
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);
 
411
        if (t_sddl == NULL)
 
412
                return -1;
 
413
        if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
 
414
                    t_sddl, SDDL_REVISION_1,
 
415
                    (PSECURITY_DESCRIPTOR *) &priv->attr.lpSecurityDescriptor,
 
416
                    NULL)) {
 
417
                os_free(t_sddl);
 
418
                wpa_printf(MSG_ERROR, "CTRL: SDDL='%s' - could not convert to "
 
419
                           "security descriptor: %d",
 
420
                           sddl, (int) GetLastError());
 
421
                return -1;
 
422
        }
 
423
        os_free(t_sddl);
 
424
 
 
425
        priv->sec_attr_set = 1;
 
426
 
 
427
        return 0;
 
428
}
 
429
 
 
430
 
 
431
static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
 
432
                                             const char *txt, size_t len)
 
433
{
 
434
        struct wpa_supplicant *wpa_s = ctx;
 
435
        if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
 
436
                return;
 
437
        wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
 
438
}
 
439
 
 
440
 
 
441
struct ctrl_iface_priv *
 
442
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
 
443
{
 
444
        struct ctrl_iface_priv *priv;
 
445
 
 
446
        priv = os_zalloc(sizeof(*priv));
 
447
        if (priv == NULL)
 
448
                return NULL;
 
449
        priv->wpa_s = wpa_s;
 
450
 
 
451
        if (wpa_s->conf->ctrl_interface == NULL)
 
452
                return priv;
 
453
 
 
454
        if (ctrl_iface_parse(priv, wpa_s->conf->ctrl_interface) < 0) {
 
455
                os_free(priv);
 
456
                return NULL;
 
457
        }
 
458
 
 
459
        if (ctrl_open_pipe(priv) < 0) {
 
460
                os_free(priv);
 
461
                return NULL;
 
462
        }
 
463
 
 
464
        wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
 
465
 
 
466
        return priv;
 
467
}
 
468
 
 
469
 
 
470
void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
 
471
{
 
472
        while (priv->ctrl_dst)
 
473
                ctrl_close_pipe(priv->ctrl_dst);
 
474
        if (priv->sec_attr_set)
 
475
                LocalFree(priv->attr.lpSecurityDescriptor);
 
476
        os_free(priv);
 
477
}
 
478
 
 
479
 
 
480
static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 
481
                                           int level, const char *buf,
 
482
                                           size_t len)
 
483
{
 
484
        struct wpa_ctrl_dst *dst, *next;
 
485
        char levelstr[10];
 
486
        int idx;
 
487
        char *sbuf;
 
488
        int llen;
 
489
        DWORD written;
 
490
 
 
491
        dst = priv->ctrl_dst;
 
492
        if (dst == NULL)
 
493
                return;
 
494
 
 
495
        os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
 
496
 
 
497
        llen = os_strlen(levelstr);
 
498
        sbuf = os_malloc(llen + len);
 
499
        if (sbuf == NULL)
 
500
                return;
 
501
 
 
502
        os_memcpy(sbuf, levelstr, llen);
 
503
        os_memcpy(sbuf + llen, buf, len);
 
504
 
 
505
        idx = 0;
 
506
        while (dst) {
 
507
                next = dst->next;
 
508
                if (dst->attached && level >= dst->debug_level) {
 
509
                        wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %p",
 
510
                                   dst);
 
511
                        if (!WriteFile(dst->pipe, sbuf, llen + len, &written,
 
512
                                       NULL)) {
 
513
                                wpa_printf(MSG_DEBUG, "CTRL: WriteFile to dst "
 
514
                                           "%p failed: %d",
 
515
                                           dst, (int) GetLastError());
 
516
                                dst->errors++;
 
517
                                if (dst->errors > 10)
 
518
                                        ctrl_close_pipe(dst);
 
519
                        } else
 
520
                                dst->errors = 0;
 
521
                }
 
522
                idx++;
 
523
                dst = next;
 
524
        }
 
525
        os_free(sbuf);
 
526
}
 
527
 
 
528
 
 
529
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
 
530
{
 
531
        wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
 
532
                   priv->wpa_s->ifname);
 
533
        if (priv->ctrl_dst == NULL)
 
534
                return;
 
535
        WaitForSingleObject(priv->ctrl_dst->pipe, INFINITE);
 
536
}
 
537
 
 
538
 
 
539
/* Global ctrl_iface */
 
540
 
 
541
struct ctrl_iface_global_priv;
 
542
 
 
543
struct wpa_global_dst {
 
544
        /* Note: OVERLAPPED must be the first member of struct wpa_global_dst
 
545
         */
 
546
        OVERLAPPED overlap;
 
547
        struct wpa_global_dst *next, *prev;
 
548
        struct ctrl_iface_global_priv *priv;
 
549
        HANDLE pipe;
 
550
        char req_buf[REQUEST_BUFSIZE];
 
551
        char *rsp_buf;
 
552
        int used;
 
553
};
 
554
 
 
555
struct ctrl_iface_global_priv {
 
556
        struct wpa_global *global;
 
557
        struct wpa_global_dst *ctrl_dst;
 
558
};
 
559
 
 
560
 
 
561
static void global_flush_broken_pipes(struct ctrl_iface_global_priv *priv)
 
562
{
 
563
        struct wpa_global_dst *dst, *next;
 
564
 
 
565
        dst = priv->ctrl_dst;
 
566
 
 
567
        while (dst) {
 
568
                next = dst->next;
 
569
                if (ctrl_broken_pipe(dst->pipe, dst->used)) {
 
570
                        wpa_printf(MSG_DEBUG, "CTRL: closing broken pipe %p",
 
571
                                   dst);
 
572
                        global_close_pipe(dst);
 
573
                }
 
574
                dst = next;
 
575
        }
 
576
}
 
577
 
 
578
 
 
579
static int global_open_pipe(struct ctrl_iface_global_priv *priv)
 
580
{
 
581
        struct wpa_global_dst *dst;
 
582
        DWORD err;
 
583
 
 
584
        dst = os_zalloc(sizeof(*dst));
 
585
        if (dst == NULL)
 
586
                return -1;
 
587
        wpa_printf(MSG_DEBUG, "CTRL: Open pipe %p", dst);
 
588
 
 
589
        dst->priv = priv;
 
590
        dst->pipe = INVALID_HANDLE_VALUE;
 
591
 
 
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());
 
596
                goto fail;
 
597
        }
 
598
 
 
599
        eloop_register_event(dst->overlap.hEvent,
 
600
                             sizeof(dst->overlap.hEvent),
 
601
                             wpa_supplicant_global_iface_receive, dst, NULL);
 
602
 
 
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,
 
606
                                    PIPE_TYPE_MESSAGE |
 
607
                                    PIPE_READMODE_MESSAGE |
 
608
                                    PIPE_WAIT,
 
609
                                    10, REPLY_BUFSIZE, REQUEST_BUFSIZE,
 
610
                                    1000, NULL);
 
611
        if (dst->pipe == INVALID_HANDLE_VALUE) {
 
612
                wpa_printf(MSG_ERROR, "CTRL: CreateNamedPipe failed: %d",
 
613
                           (int) GetLastError());
 
614
                goto fail;
 
615
        }
 
616
 
 
617
        if (ConnectNamedPipe(dst->pipe, &dst->overlap)) {
 
618
                wpa_printf(MSG_ERROR, "CTRL: ConnectNamedPipe failed: %d",
 
619
                           (int) GetLastError());
 
620
                CloseHandle(dst->pipe);
 
621
                os_free(dst);
 
622
                return -1;
 
623
        }
 
624
 
 
625
        err = GetLastError();
 
626
        switch (err) {
 
627
        case ERROR_IO_PENDING:
 
628
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: connection in "
 
629
                           "progress");
 
630
                break;
 
631
        case ERROR_PIPE_CONNECTED:
 
632
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe: already "
 
633
                           "connected");
 
634
                if (SetEvent(dst->overlap.hEvent))
 
635
                        break;
 
636
                /* fall through */
 
637
        default:
 
638
                wpa_printf(MSG_DEBUG, "CTRL: ConnectNamedPipe error: %d",
 
639
                           (int) err);
 
640
                CloseHandle(dst->pipe);
 
641
                os_free(dst);
 
642
                return -1;
 
643
        }
 
644
 
 
645
        dst->next = priv->ctrl_dst;
 
646
        if (dst->next)
 
647
                dst->next->prev = dst;
 
648
        priv->ctrl_dst = dst;
 
649
 
 
650
        return 0;
 
651
 
 
652
fail:
 
653
        global_close_pipe(dst);
 
654
        return -1;
 
655
}
 
656
 
 
657
 
 
658
static void global_close_pipe(struct wpa_global_dst *dst)
 
659
{
 
660
        wpa_printf(MSG_DEBUG, "CTRL: close pipe %p", dst);
 
661
 
 
662
        if (dst->overlap.hEvent) {
 
663
                eloop_unregister_event(dst->overlap.hEvent,
 
664
                                       sizeof(dst->overlap.hEvent));
 
665
                CloseHandle(dst->overlap.hEvent);
 
666
        }
 
667
 
 
668
        if (dst->pipe != INVALID_HANDLE_VALUE) {
 
669
                /*
 
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);
 
674
                 */
 
675
                CloseHandle(dst->pipe);
 
676
        }
 
677
 
 
678
        if (dst->prev)
 
679
                dst->prev->next = dst->next;
 
680
        else
 
681
                dst->priv->ctrl_dst = dst->next;
 
682
        if (dst->next)
 
683
                dst->next->prev = dst->prev;
 
684
 
 
685
        os_free(dst->rsp_buf);
 
686
        os_free(dst);
 
687
}
 
688
 
 
689
 
 
690
static VOID WINAPI global_iface_write_completed(DWORD err, DWORD bytes,
 
691
                                                LPOVERLAPPED overlap)
 
692
{
 
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);
 
696
        if (err) {
 
697
                global_close_pipe(dst);
 
698
                return;
 
699
        }
 
700
 
 
701
        os_free(dst->rsp_buf);
 
702
        dst->rsp_buf = NULL;
 
703
 
 
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.. */
 
712
                return;
 
713
        }
 
714
        wpa_printf(MSG_DEBUG, "CTRL: Overlapped read started for %p", dst);
 
715
}
 
716
 
 
717
 
 
718
static void wpa_supplicant_global_iface_rx(struct wpa_global_dst *dst,
 
719
                                           size_t len)
 
720
{
 
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;
 
725
 
 
726
        dst->used = 1;
 
727
        if (len >= REQUEST_BUFSIZE)
 
728
                len = REQUEST_BUFSIZE - 1;
 
729
        buf[len] = '\0';
 
730
 
 
731
        reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
 
732
                                                         &reply_len);
 
733
        if (reply) {
 
734
                send_buf = reply;
 
735
                send_len = reply_len;
 
736
        } else if (reply_len) {
 
737
                send_buf = "FAIL\n";
 
738
                send_len = 5;
 
739
        } else {
 
740
                os_free(dst->rsp_buf);
 
741
                dst->rsp_buf = NULL;
 
742
                return;
 
743
        }
 
744
 
 
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);
 
749
                os_free(reply);
 
750
                return;
 
751
        }
 
752
        os_memcpy(dst->rsp_buf, send_buf, send_len);
 
753
        os_free(reply);
 
754
 
 
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);
 
760
        } else {
 
761
                wpa_printf(MSG_DEBUG, "CTRL: Overlapped write started for %p",
 
762
                           dst);
 
763
        }
 
764
}
 
765
 
 
766
 
 
767
static VOID WINAPI global_iface_read_completed(DWORD err, DWORD bytes,
 
768
                                               LPOVERLAPPED overlap)
 
769
{
 
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);
 
775
}
 
776
 
 
777
 
 
778
static void wpa_supplicant_global_iface_receive(void *eloop_data,
 
779
                                                void *user_ctx)
 
780
{
 
781
        struct wpa_global_dst *dst = eloop_data;
 
782
        struct ctrl_iface_global_priv *priv = dst->priv;
 
783
        DWORD bytes;
 
784
 
 
785
        wpa_printf(MSG_DEBUG, "CTRL: wpa_supplicant_global_iface_receive");
 
786
        ResetEvent(dst->overlap.hEvent);
 
787
 
 
788
        if (!GetOverlappedResult(dst->pipe, &dst->overlap, &bytes, FALSE)) {
 
789
                wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult failed: %d",
 
790
                           (int) GetLastError());
 
791
                return;
 
792
        }
 
793
        wpa_printf(MSG_DEBUG, "CTRL: GetOverlappedResult: New client "
 
794
                   "connected");
 
795
 
 
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");
 
799
                return;
 
800
        }
 
801
 
 
802
        /* Use write completion function to start reading a command */
 
803
        global_iface_write_completed(0, 0, &dst->overlap);
 
804
 
 
805
        global_flush_broken_pipes(priv);
 
806
}
 
807
 
 
808
 
 
809
struct ctrl_iface_global_priv *
 
810
wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
 
811
{
 
812
        struct ctrl_iface_global_priv *priv;
 
813
 
 
814
        priv = os_zalloc(sizeof(*priv));
 
815
        if (priv == NULL)
 
816
                return NULL;
 
817
        priv->global = global;
 
818
 
 
819
        if (global_open_pipe(priv) < 0) {
 
820
                os_free(priv);
 
821
                return NULL;
 
822
        }
 
823
 
 
824
        return priv;
 
825
}
 
826
 
 
827
 
 
828
void
 
829
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
 
830
{
 
831
        while (priv->ctrl_dst)
 
832
                global_close_pipe(priv->ctrl_dst);
 
833
        os_free(priv);
 
834
}