]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/common/wpa_ctrl.c
Make UNIX socket non-blocking for ctrl_iface
[thirdparty/hostap.git] / src / common / wpa_ctrl.c
1 /*
2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #ifdef CONFIG_CTRL_IFACE
12
13 #ifdef CONFIG_CTRL_IFACE_UNIX
14 #include <sys/un.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #endif /* CONFIG_CTRL_IFACE_UNIX */
18
19 #ifdef ANDROID
20 #include <dirent.h>
21 #include <cutils/sockets.h>
22 #include "private/android_filesystem_config.h"
23 #endif /* ANDROID */
24
25 #include "wpa_ctrl.h"
26 #include "common.h"
27
28
29 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
30 #define CTRL_IFACE_SOCKET
31 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
32
33
34 /**
35 * struct wpa_ctrl - Internal structure for control interface library
36 *
37 * This structure is used by the wpa_supplicant/hostapd control interface
38 * library to store internal data. Programs using the library should not touch
39 * this data directly. They can only use the pointer to the data structure as
40 * an identifier for the control interface connection and use this as one of
41 * the arguments for most of the control interface library functions.
42 */
43 struct wpa_ctrl {
44 #ifdef CONFIG_CTRL_IFACE_UDP
45 int s;
46 struct sockaddr_in local;
47 struct sockaddr_in dest;
48 char *cookie;
49 #endif /* CONFIG_CTRL_IFACE_UDP */
50 #ifdef CONFIG_CTRL_IFACE_UNIX
51 int s;
52 struct sockaddr_un local;
53 struct sockaddr_un dest;
54 #endif /* CONFIG_CTRL_IFACE_UNIX */
55 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
56 HANDLE pipe;
57 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
58 };
59
60
61 #ifdef CONFIG_CTRL_IFACE_UNIX
62
63 #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR
64 #define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp"
65 #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */
66 #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX
67 #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_"
68 #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */
69
70
71 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
72 {
73 struct wpa_ctrl *ctrl;
74 static int counter = 0;
75 int ret;
76 size_t res;
77 int tries = 0;
78 int flags;
79
80 ctrl = os_malloc(sizeof(*ctrl));
81 if (ctrl == NULL)
82 return NULL;
83 os_memset(ctrl, 0, sizeof(*ctrl));
84
85 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
86 if (ctrl->s < 0) {
87 os_free(ctrl);
88 return NULL;
89 }
90
91 ctrl->local.sun_family = AF_UNIX;
92 counter++;
93 try_again:
94 ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
95 CONFIG_CTRL_IFACE_CLIENT_DIR "/"
96 CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d",
97 (int) getpid(), counter);
98 if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
99 close(ctrl->s);
100 os_free(ctrl);
101 return NULL;
102 }
103 tries++;
104 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
105 sizeof(ctrl->local)) < 0) {
106 if (errno == EADDRINUSE && tries < 2) {
107 /*
108 * getpid() returns unique identifier for this instance
109 * of wpa_ctrl, so the existing socket file must have
110 * been left by unclean termination of an earlier run.
111 * Remove the file and try again.
112 */
113 unlink(ctrl->local.sun_path);
114 goto try_again;
115 }
116 close(ctrl->s);
117 os_free(ctrl);
118 return NULL;
119 }
120
121 #ifdef ANDROID
122 chmod(ctrl->local.sun_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
123 chown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI);
124 /*
125 * If the ctrl_path isn't an absolute pathname, assume that
126 * it's the name of a socket in the Android reserved namespace.
127 * Otherwise, it's a normal UNIX domain socket appearing in the
128 * filesystem.
129 */
130 if (ctrl_path != NULL && *ctrl_path != '/') {
131 char buf[21];
132 os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path);
133 if (socket_local_client_connect(
134 ctrl->s, buf,
135 ANDROID_SOCKET_NAMESPACE_RESERVED,
136 SOCK_DGRAM) < 0) {
137 close(ctrl->s);
138 unlink(ctrl->local.sun_path);
139 os_free(ctrl);
140 return NULL;
141 }
142 return ctrl;
143 }
144 #endif /* ANDROID */
145
146 ctrl->dest.sun_family = AF_UNIX;
147 res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
148 sizeof(ctrl->dest.sun_path));
149 if (res >= sizeof(ctrl->dest.sun_path)) {
150 close(ctrl->s);
151 os_free(ctrl);
152 return NULL;
153 }
154 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
155 sizeof(ctrl->dest)) < 0) {
156 close(ctrl->s);
157 unlink(ctrl->local.sun_path);
158 os_free(ctrl);
159 return NULL;
160 }
161
162 /*
163 * Make socket non-blocking so that we don't hang forever if
164 * target dies unexpectedly.
165 */
166 flags = fcntl(ctrl->s, F_GETFL);
167 if (flags >= 0) {
168 flags |= O_NONBLOCK;
169 if (fcntl(ctrl->s, F_SETFL, flags) < 0) {
170 perror("fcntl(ctrl->s, O_NONBLOCK)");
171 /* Not fatal, continue on.*/
172 }
173 }
174
175 return ctrl;
176 }
177
178
179 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
180 {
181 if (ctrl == NULL)
182 return;
183 unlink(ctrl->local.sun_path);
184 if (ctrl->s >= 0)
185 close(ctrl->s);
186 os_free(ctrl);
187 }
188
189
190 #ifdef ANDROID
191 /**
192 * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
193 * may be left over from clients that were previously connected to
194 * wpa_supplicant. This keeps these files from being orphaned in the
195 * event of crashes that prevented them from being removed as part
196 * of the normal orderly shutdown.
197 */
198 void wpa_ctrl_cleanup(void)
199 {
200 DIR *dir;
201 struct dirent entry;
202 struct dirent *result;
203 size_t dirnamelen;
204 int prefixlen = os_strlen(CONFIG_CTRL_IFACE_CLIENT_PREFIX);
205 size_t maxcopy;
206 char pathname[PATH_MAX];
207 char *namep;
208
209 if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL)
210 return;
211
212 dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/",
213 CONFIG_CTRL_IFACE_CLIENT_DIR);
214 if (dirnamelen >= sizeof(pathname)) {
215 closedir(dir);
216 return;
217 }
218 namep = pathname + dirnamelen;
219 maxcopy = PATH_MAX - dirnamelen;
220 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
221 if (os_strncmp(entry.d_name, CONFIG_CTRL_IFACE_CLIENT_PREFIX,
222 prefixlen) == 0) {
223 if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy)
224 unlink(pathname);
225 }
226 }
227 closedir(dir);
228 }
229 #endif /* ANDROID */
230
231 #else /* CONFIG_CTRL_IFACE_UNIX */
232
233 #ifdef ANDROID
234 void wpa_ctrl_cleanup(void)
235 {
236 }
237 #endif /* ANDROID */
238
239 #endif /* CONFIG_CTRL_IFACE_UNIX */
240
241
242 #ifdef CONFIG_CTRL_IFACE_UDP
243
244 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
245 {
246 struct wpa_ctrl *ctrl;
247 char buf[128];
248 size_t len;
249
250 ctrl = os_malloc(sizeof(*ctrl));
251 if (ctrl == NULL)
252 return NULL;
253 os_memset(ctrl, 0, sizeof(*ctrl));
254
255 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
256 if (ctrl->s < 0) {
257 perror("socket");
258 os_free(ctrl);
259 return NULL;
260 }
261
262 ctrl->local.sin_family = AF_INET;
263 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
264 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
265 sizeof(ctrl->local)) < 0) {
266 close(ctrl->s);
267 os_free(ctrl);
268 return NULL;
269 }
270
271 ctrl->dest.sin_family = AF_INET;
272 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
273 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
274 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
275 sizeof(ctrl->dest)) < 0) {
276 perror("connect");
277 close(ctrl->s);
278 os_free(ctrl);
279 return NULL;
280 }
281
282 len = sizeof(buf) - 1;
283 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
284 buf[len] = '\0';
285 ctrl->cookie = os_strdup(buf);
286 }
287
288 return ctrl;
289 }
290
291
292 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
293 {
294 close(ctrl->s);
295 os_free(ctrl->cookie);
296 os_free(ctrl);
297 }
298
299 #endif /* CONFIG_CTRL_IFACE_UDP */
300
301
302 #ifdef CTRL_IFACE_SOCKET
303 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
304 char *reply, size_t *reply_len,
305 void (*msg_cb)(char *msg, size_t len))
306 {
307 struct timeval tv;
308 struct os_time started_at;
309 int res;
310 fd_set rfds;
311 const char *_cmd;
312 char *cmd_buf = NULL;
313 size_t _cmd_len;
314
315 #ifdef CONFIG_CTRL_IFACE_UDP
316 if (ctrl->cookie) {
317 char *pos;
318 _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
319 cmd_buf = os_malloc(_cmd_len);
320 if (cmd_buf == NULL)
321 return -1;
322 _cmd = cmd_buf;
323 pos = cmd_buf;
324 os_strlcpy(pos, ctrl->cookie, _cmd_len);
325 pos += os_strlen(ctrl->cookie);
326 *pos++ = ' ';
327 os_memcpy(pos, cmd, cmd_len);
328 } else
329 #endif /* CONFIG_CTRL_IFACE_UDP */
330 {
331 _cmd = cmd;
332 _cmd_len = cmd_len;
333 }
334
335 errno = 0;
336 started_at.sec = 0;
337 started_at.usec = 0;
338 retry_send:
339 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
340 if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK)
341 {
342 /*
343 * Must be a non-blocking socket... Try for a bit
344 * longer before giving up.
345 */
346 if (started_at.sec == 0)
347 os_get_time(&started_at);
348 else {
349 struct os_time n;
350 os_get_time(&n);
351 /* Try for a few seconds. */
352 if (n.sec > started_at.sec + 5)
353 goto send_err;
354 }
355 os_sleep(1, 0);
356 goto retry_send;
357 }
358 send_err:
359 os_free(cmd_buf);
360 return -1;
361 }
362 os_free(cmd_buf);
363
364 for (;;) {
365 tv.tv_sec = 10;
366 tv.tv_usec = 0;
367 FD_ZERO(&rfds);
368 FD_SET(ctrl->s, &rfds);
369 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
370 if (res < 0)
371 return res;
372 if (FD_ISSET(ctrl->s, &rfds)) {
373 res = recv(ctrl->s, reply, *reply_len, 0);
374 if (res < 0)
375 return res;
376 if (res > 0 && reply[0] == '<') {
377 /* This is an unsolicited message from
378 * wpa_supplicant, not the reply to the
379 * request. Use msg_cb to report this to the
380 * caller. */
381 if (msg_cb) {
382 /* Make sure the message is nul
383 * terminated. */
384 if ((size_t) res == *reply_len)
385 res = (*reply_len) - 1;
386 reply[res] = '\0';
387 msg_cb(reply, res);
388 }
389 continue;
390 }
391 *reply_len = res;
392 break;
393 } else {
394 return -2;
395 }
396 }
397 return 0;
398 }
399 #endif /* CTRL_IFACE_SOCKET */
400
401
402 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
403 {
404 char buf[10];
405 int ret;
406 size_t len = 10;
407
408 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
409 buf, &len, NULL);
410 if (ret < 0)
411 return ret;
412 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
413 return 0;
414 return -1;
415 }
416
417
418 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
419 {
420 return wpa_ctrl_attach_helper(ctrl, 1);
421 }
422
423
424 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
425 {
426 return wpa_ctrl_attach_helper(ctrl, 0);
427 }
428
429
430 #ifdef CTRL_IFACE_SOCKET
431
432 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
433 {
434 int res;
435
436 res = recv(ctrl->s, reply, *reply_len, 0);
437 if (res < 0)
438 return res;
439 *reply_len = res;
440 return 0;
441 }
442
443
444 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
445 {
446 struct timeval tv;
447 fd_set rfds;
448 tv.tv_sec = 0;
449 tv.tv_usec = 0;
450 FD_ZERO(&rfds);
451 FD_SET(ctrl->s, &rfds);
452 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
453 return FD_ISSET(ctrl->s, &rfds);
454 }
455
456
457 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
458 {
459 return ctrl->s;
460 }
461
462 #endif /* CTRL_IFACE_SOCKET */
463
464
465 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
466
467 #ifndef WPA_SUPPLICANT_NAMED_PIPE
468 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
469 #endif
470 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
471
472 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
473 {
474 struct wpa_ctrl *ctrl;
475 DWORD mode;
476 TCHAR name[256];
477 int i, ret;
478
479 ctrl = os_malloc(sizeof(*ctrl));
480 if (ctrl == NULL)
481 return NULL;
482 os_memset(ctrl, 0, sizeof(*ctrl));
483
484 #ifdef UNICODE
485 if (ctrl_path == NULL)
486 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
487 else
488 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
489 ctrl_path);
490 #else /* UNICODE */
491 if (ctrl_path == NULL)
492 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
493 else
494 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
495 ctrl_path);
496 #endif /* UNICODE */
497 if (ret < 0 || ret >= 256) {
498 os_free(ctrl);
499 return NULL;
500 }
501
502 for (i = 0; i < 10; i++) {
503 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
504 NULL, OPEN_EXISTING, 0, NULL);
505 /*
506 * Current named pipe server side in wpa_supplicant is
507 * re-opening the pipe for new clients only after the previous
508 * one is taken into use. This leaves a small window for race
509 * conditions when two connections are being opened at almost
510 * the same time. Retry if that was the case.
511 */
512 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
513 GetLastError() != ERROR_PIPE_BUSY)
514 break;
515 WaitNamedPipe(name, 1000);
516 }
517 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
518 os_free(ctrl);
519 return NULL;
520 }
521
522 mode = PIPE_READMODE_MESSAGE;
523 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
524 CloseHandle(ctrl->pipe);
525 os_free(ctrl);
526 return NULL;
527 }
528
529 return ctrl;
530 }
531
532
533 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
534 {
535 CloseHandle(ctrl->pipe);
536 os_free(ctrl);
537 }
538
539
540 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
541 char *reply, size_t *reply_len,
542 void (*msg_cb)(char *msg, size_t len))
543 {
544 DWORD written;
545 DWORD readlen = *reply_len;
546
547 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
548 return -1;
549
550 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
551 return -1;
552 *reply_len = readlen;
553
554 return 0;
555 }
556
557
558 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
559 {
560 DWORD len = *reply_len;
561 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
562 return -1;
563 *reply_len = len;
564 return 0;
565 }
566
567
568 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
569 {
570 DWORD left;
571
572 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
573 return -1;
574 return left ? 1 : 0;
575 }
576
577
578 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
579 {
580 return -1;
581 }
582
583 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
584
585 #endif /* CONFIG_CTRL_IFACE */