2 * wpa_supplicant/hostapd control interface library
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifdef CONFIG_CTRL_IFACE
13 #ifdef CONFIG_CTRL_IFACE_UNIX
19 #endif /* CONFIG_CTRL_IFACE_UNIX */
20 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
22 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
27 #include <cutils/sockets.h>
28 #include "private/android_filesystem_config.h"
31 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
33 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
39 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
40 #define CTRL_IFACE_SOCKET
41 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
45 * struct wpa_ctrl - Internal structure for control interface library
47 * This structure is used by the wpa_supplicant/hostapd control interface
48 * library to store internal data. Programs using the library should not touch
49 * this data directly. They can only use the pointer to the data structure as
50 * an identifier for the control interface connection and use this as one of
51 * the arguments for most of the control interface library functions.
54 #ifdef CONFIG_CTRL_IFACE_UDP
56 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
57 struct sockaddr_in6 local;
58 struct sockaddr_in6 dest;
59 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
60 struct sockaddr_in local;
61 struct sockaddr_in dest;
62 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
66 #endif /* CONFIG_CTRL_IFACE_UDP */
67 #ifdef CONFIG_CTRL_IFACE_UNIX
69 struct sockaddr_un local;
70 struct sockaddr_un dest;
71 #endif /* CONFIG_CTRL_IFACE_UNIX */
72 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
74 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
78 #ifdef CONFIG_CTRL_IFACE_UNIX
80 #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR
81 #define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp"
82 #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */
83 #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX
84 #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_"
85 #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */
88 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
90 return wpa_ctrl_open2(ctrl_path, NULL);
94 struct wpa_ctrl * wpa_ctrl_open2(const char *ctrl_path,
97 struct wpa_ctrl *ctrl;
98 static int counter = 0;
104 if (ctrl_path == NULL)
107 ctrl = os_zalloc(sizeof(*ctrl));
111 ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
117 ctrl->local.sun_family = AF_UNIX;
120 if (cli_path && cli_path[0] == '/') {
121 ret = os_snprintf(ctrl->local.sun_path,
122 sizeof(ctrl->local.sun_path),
123 "%s/" CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d",
124 cli_path, (int) getpid(), counter);
126 ret = os_snprintf(ctrl->local.sun_path,
127 sizeof(ctrl->local.sun_path),
128 CONFIG_CTRL_IFACE_CLIENT_DIR "/"
129 CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d",
130 (int) getpid(), counter);
132 if (os_snprintf_error(sizeof(ctrl->local.sun_path), ret)) {
139 /* Set client socket file permissions so that bind() creates the client
140 * socket with these permissions and there is no need to try to change
141 * them with chmod() after bind() which would have potential issues with
142 * race conditions. These permissions are needed to make sure the server
143 * side (wpa_supplicant or hostapd) can reply to the control interface
146 * The lchown() calls below after bind() are also part of the needed
147 * operations to allow the response to go through. Those are using the
148 * no-deference-symlinks version to avoid races. */
149 fchmod(ctrl->s, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
151 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
152 sizeof(ctrl->local)) < 0) {
153 if (errno == EADDRINUSE && tries < 2) {
155 * getpid() returns unique identifier for this instance
156 * of wpa_ctrl, so the existing socket file must have
157 * been left by unclean termination of an earlier run.
158 * Remove the file and try again.
160 unlink(ctrl->local.sun_path);
169 /* Set group even if we do not have privileges to change owner */
170 lchown(ctrl->local.sun_path, -1, AID_WIFI);
171 lchown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI);
173 if (os_strncmp(ctrl_path, "@android:", 9) == 0) {
174 if (socket_local_client_connect(
175 ctrl->s, ctrl_path + 9,
176 ANDROID_SOCKET_NAMESPACE_RESERVED,
179 unlink(ctrl->local.sun_path);
187 * If the ctrl_path isn't an absolute pathname, assume that
188 * it's the name of a socket in the Android reserved namespace.
189 * Otherwise, it's a normal UNIX domain socket appearing in the
192 if (*ctrl_path != '/') {
194 os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path);
195 if (socket_local_client_connect(
197 ANDROID_SOCKET_NAMESPACE_RESERVED,
200 unlink(ctrl->local.sun_path);
208 ctrl->dest.sun_family = AF_UNIX;
209 if (os_strncmp(ctrl_path, "@abstract:", 10) == 0) {
210 ctrl->dest.sun_path[0] = '\0';
211 os_strlcpy(ctrl->dest.sun_path + 1, ctrl_path + 10,
212 sizeof(ctrl->dest.sun_path) - 1);
214 res = os_strlcpy(ctrl->dest.sun_path, ctrl_path,
215 sizeof(ctrl->dest.sun_path));
216 if (res >= sizeof(ctrl->dest.sun_path)) {
222 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
223 sizeof(ctrl->dest)) < 0) {
225 unlink(ctrl->local.sun_path);
231 * Make socket non-blocking so that we don't hang forever if
232 * target dies unexpectedly.
234 flags = fcntl(ctrl->s, F_GETFL);
237 if (fcntl(ctrl->s, F_SETFL, flags) < 0) {
238 perror("fcntl(ctrl->s, O_NONBLOCK)");
239 /* Not fatal, continue on.*/
247 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
251 unlink(ctrl->local.sun_path);
260 * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
261 * may be left over from clients that were previously connected to
262 * wpa_supplicant. This keeps these files from being orphaned in the
263 * event of crashes that prevented them from being removed as part
264 * of the normal orderly shutdown.
266 void wpa_ctrl_cleanup(void)
270 struct dirent *result;
273 char pathname[PATH_MAX];
276 if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL)
279 dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/",
280 CONFIG_CTRL_IFACE_CLIENT_DIR);
281 if (dirnamelen >= sizeof(pathname)) {
285 namep = pathname + dirnamelen;
286 maxcopy = PATH_MAX - dirnamelen;
287 while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
288 if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy)
295 #else /* CONFIG_CTRL_IFACE_UNIX */
298 void wpa_ctrl_cleanup(void)
303 #endif /* CONFIG_CTRL_IFACE_UNIX */
306 #ifdef CONFIG_CTRL_IFACE_UDP
308 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
310 struct wpa_ctrl *ctrl;
313 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
315 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
317 ctrl = os_zalloc(sizeof(*ctrl));
321 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
322 ctrl->s = socket(PF_INET6, SOCK_DGRAM, 0);
323 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
324 ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
325 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
332 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
333 ctrl->local.sin6_family = AF_INET6;
334 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
335 ctrl->local.sin6_addr = in6addr_any;
336 #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
337 inet_pton(AF_INET6, "::1", &ctrl->local.sin6_addr);
338 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
339 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
340 ctrl->local.sin_family = AF_INET;
341 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
342 ctrl->local.sin_addr.s_addr = INADDR_ANY;
343 #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
344 ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
345 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
346 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
348 if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
349 sizeof(ctrl->local)) < 0) {
355 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
356 ctrl->dest.sin6_family = AF_INET6;
357 inet_pton(AF_INET6, "::1", &ctrl->dest.sin6_addr);
358 ctrl->dest.sin6_port = htons(WPA_CTRL_IFACE_PORT);
359 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
360 ctrl->dest.sin_family = AF_INET;
361 ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
362 ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
363 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
365 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
369 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
372 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
374 name = os_strdup(ctrl_path);
380 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
381 port = os_strchr(name, ',');
382 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
383 port = os_strchr(name, ':');
384 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
387 port_id = atoi(&port[1]);
390 port_id = WPA_CTRL_IFACE_PORT;
392 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
393 scope = os_strchr(name, '%');
395 scope_id = if_nametoindex(&scope[1]);
398 h = gethostbyname2(name, AF_INET6);
399 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
400 h = gethostbyname(name);
401 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
402 ctrl->remote_ip = os_strdup(name);
405 perror("gethostbyname");
407 os_free(ctrl->remote_ip);
411 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
412 ctrl->dest.sin6_scope_id = scope_id;
413 ctrl->dest.sin6_port = htons(port_id);
414 os_memcpy(&ctrl->dest.sin6_addr, h->h_addr, h->h_length);
415 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
416 ctrl->dest.sin_port = htons(port_id);
417 os_memcpy(&ctrl->dest.sin_addr.s_addr, h->h_addr, h->h_length);
418 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
420 ctrl->remote_ip = os_strdup("localhost");
421 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
423 if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
424 sizeof(ctrl->dest)) < 0) {
425 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
426 char addr[INET6_ADDRSTRLEN];
427 wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
428 inet_ntop(AF_INET6, &ctrl->dest.sin6_addr, addr,
430 ntohs(ctrl->dest.sin6_port),
432 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
433 wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
434 inet_ntoa(ctrl->dest.sin_addr),
435 ntohs(ctrl->dest.sin_port),
437 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
439 os_free(ctrl->remote_ip);
444 len = sizeof(buf) - 1;
445 if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
447 ctrl->cookie = os_strdup(buf);
450 if (wpa_ctrl_request(ctrl, "IFNAME", 6, buf, &len, NULL) == 0) {
452 ctrl->remote_ifname = os_strdup(buf);
459 char * wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl)
461 #define WPA_CTRL_MAX_PS_NAME 100
462 static char ps[WPA_CTRL_MAX_PS_NAME] = {};
463 os_snprintf(ps, WPA_CTRL_MAX_PS_NAME, "%s/%s",
464 ctrl->remote_ip, ctrl->remote_ifname);
469 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
472 os_free(ctrl->cookie);
473 os_free(ctrl->remote_ifname);
474 os_free(ctrl->remote_ip);
478 #endif /* CONFIG_CTRL_IFACE_UDP */
481 #ifdef CTRL_IFACE_SOCKET
482 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
483 char *reply, size_t *reply_len,
484 void (*msg_cb)(char *msg, size_t len))
487 struct os_reltime started_at;
491 char *cmd_buf = NULL;
494 #ifdef CONFIG_CTRL_IFACE_UDP
497 _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len;
498 cmd_buf = os_malloc(_cmd_len);
503 os_strlcpy(pos, ctrl->cookie, _cmd_len);
504 pos += os_strlen(ctrl->cookie);
506 os_memcpy(pos, cmd, cmd_len);
508 #endif /* CONFIG_CTRL_IFACE_UDP */
518 if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
519 if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK)
522 * Must be a non-blocking socket... Try for a bit
523 * longer before giving up.
525 if (started_at.sec == 0)
526 os_get_reltime(&started_at);
530 /* Try for a few seconds. */
531 if (os_reltime_expired(&n, &started_at, 5))
547 FD_SET(ctrl->s, &rfds);
548 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
549 if (res < 0 && errno == EINTR)
553 if (FD_ISSET(ctrl->s, &rfds)) {
554 res = recv(ctrl->s, reply, *reply_len, 0);
557 if ((res > 0 && reply[0] == '<') ||
558 (res > 6 && strncmp(reply, "IFNAME=", 7) == 0)) {
559 /* This is an unsolicited message from
560 * wpa_supplicant, not the reply to the
561 * request. Use msg_cb to report this to the
564 /* Make sure the message is nul
566 if ((size_t) res == *reply_len)
567 res = (*reply_len) - 1;
581 #endif /* CTRL_IFACE_SOCKET */
584 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
590 ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
594 if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
600 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
602 return wpa_ctrl_attach_helper(ctrl, 1);
606 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
608 return wpa_ctrl_attach_helper(ctrl, 0);
612 #ifdef CTRL_IFACE_SOCKET
614 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
618 res = recv(ctrl->s, reply, *reply_len, 0);
626 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
633 FD_SET(ctrl->s, &rfds);
634 select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
635 return FD_ISSET(ctrl->s, &rfds);
639 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
644 #endif /* CTRL_IFACE_SOCKET */
647 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
649 #ifndef WPA_SUPPLICANT_NAMED_PIPE
650 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
652 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
654 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
656 struct wpa_ctrl *ctrl;
661 ctrl = os_malloc(sizeof(*ctrl));
664 os_memset(ctrl, 0, sizeof(*ctrl));
667 if (ctrl_path == NULL)
668 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX);
670 ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
673 if (ctrl_path == NULL)
674 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX);
676 ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
679 if (os_snprintf_error(256, ret)) {
684 for (i = 0; i < 10; i++) {
685 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
686 NULL, OPEN_EXISTING, 0, NULL);
688 * Current named pipe server side in wpa_supplicant is
689 * re-opening the pipe for new clients only after the previous
690 * one is taken into use. This leaves a small window for race
691 * conditions when two connections are being opened at almost
692 * the same time. Retry if that was the case.
694 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
695 GetLastError() != ERROR_PIPE_BUSY)
697 WaitNamedPipe(name, 1000);
699 if (ctrl->pipe == INVALID_HANDLE_VALUE) {
704 mode = PIPE_READMODE_MESSAGE;
705 if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
706 CloseHandle(ctrl->pipe);
715 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
717 CloseHandle(ctrl->pipe);
722 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
723 char *reply, size_t *reply_len,
724 void (*msg_cb)(char *msg, size_t len))
727 DWORD readlen = *reply_len;
729 if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
732 if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
734 *reply_len = readlen;
740 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
742 DWORD len = *reply_len;
743 if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
750 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
754 if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
760 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
765 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
767 #endif /* CONFIG_CTRL_IFACE */