18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_supplicant/hostapd control interface library 38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi> 48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license. 6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details. 78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h" 108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE 128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UNIX 148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <sys/un.h> 1561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include <unistd.h> 1661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include <fcntl.h> 178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UNIX */ 1861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 1961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include <netdb.h> 2061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef ANDROID 231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include <dirent.h> 24db23819096e5ce4de872fa2346de7070eada297bMark Salyzyn#include <sys/stat.h> 258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <cutils/sockets.h> 268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "private/android_filesystem_config.h" 278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* ANDROID */ 288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 29f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 30f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#include <net/if.h> 31f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 32f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt 338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wpa_ctrl.h" 348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h" 358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP) 388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define CTRL_IFACE_SOCKET 398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */ 408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/** 438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * struct wpa_ctrl - Internal structure for control interface library 448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This structure is used by the wpa_supplicant/hostapd control interface 468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * library to store internal data. Programs using the library should not touch 478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * this data directly. They can only use the pointer to the data structure as 488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * an identifier for the control interface connection and use this as one of 498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the arguments for most of the control interface library functions. 508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_ctrl { 528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP 538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int s; 54f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 55f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt struct sockaddr_in6 local; 56f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt struct sockaddr_in6 dest; 57f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct sockaddr_in local; 598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct sockaddr_in dest; 60f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char *cookie; 6261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt char *remote_ifname; 6361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt char *remote_ip; 648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP */ 658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UNIX 668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int s; 678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct sockaddr_un local; 688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct sockaddr_un dest; 698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UNIX */ 708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE 718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt HANDLE pipe; 728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ 738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}; 748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UNIX 778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef CONFIG_CTRL_IFACE_CLIENT_DIR 798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define CONFIG_CTRL_IFACE_CLIENT_DIR "/tmp" 808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */ 818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX 828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wpa_ctrl_" 838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */ 848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 88d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt return wpa_ctrl_open2(ctrl_path, NULL); 89d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt} 90d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt 91d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt 92d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidtstruct wpa_ctrl * wpa_ctrl_open2(const char *ctrl_path, 93d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt const char *cli_path) 94d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt{ 958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct wpa_ctrl *ctrl; 968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt static int counter = 0; 978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int ret; 988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t res; 998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int tries = 0; 10061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt int flags; 1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 102b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (ctrl_path == NULL) 103b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt return NULL; 104b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt 105fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt ctrl = os_zalloc(sizeof(*ctrl)); 1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl == NULL) 1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); 1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->s < 0) { 1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->local.sun_family = AF_UNIX; 1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt counter++; 1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidttry_again: 118d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt if (cli_path && cli_path[0] == '/') { 119d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt ret = os_snprintf(ctrl->local.sun_path, 120d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt sizeof(ctrl->local.sun_path), 121d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt "%s/" CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", 122d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt cli_path, (int) getpid(), counter); 123d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt } else { 124d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt ret = os_snprintf(ctrl->local.sun_path, 125d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt sizeof(ctrl->local.sun_path), 126d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt CONFIG_CTRL_IFACE_CLIENT_DIR "/" 127d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", 128d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt (int) getpid(), counter); 129d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt } 130fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt if (os_snprintf_error(sizeof(ctrl->local.sun_path), ret)) { 1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt tries++; 1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, 1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt sizeof(ctrl->local)) < 0) { 1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (errno == EADDRINUSE && tries < 2) { 1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* 1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * getpid() returns unique identifier for this instance 1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * of wpa_ctrl, so the existing socket file must have 1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * been left by unclean termination of an earlier run. 1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Remove the file and try again. 1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unlink(ctrl->local.sun_path); 1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt goto try_again; 1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef ANDROID 1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt chmod(ctrl->local.sun_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 155d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt /* Set group even if we do not have privileges to change owner */ 156d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt chown(ctrl->local.sun_path, -1, AID_WIFI); 1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt chown(ctrl->local.sun_path, AID_SYSTEM, AID_WIFI); 158b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt 159b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (os_strncmp(ctrl_path, "@android:", 9) == 0) { 160b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (socket_local_client_connect( 161b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt ctrl->s, ctrl_path + 9, 162b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt ANDROID_SOCKET_NAMESPACE_RESERVED, 163b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt SOCK_DGRAM) < 0) { 164b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt close(ctrl->s); 165b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt unlink(ctrl->local.sun_path); 166b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt os_free(ctrl); 167b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt return NULL; 168b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt } 169b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt return ctrl; 170b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt } 171b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt 1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* 1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * If the ctrl_path isn't an absolute pathname, assume that 1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * it's the name of a socket in the Android reserved namespace. 1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Otherwise, it's a normal UNIX domain socket appearing in the 1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * filesystem. 1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 178b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (*ctrl_path != '/') { 1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char buf[21]; 1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_snprintf(buf, sizeof(buf), "wpa_%s", ctrl_path); 1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (socket_local_client_connect( 1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->s, buf, 1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ANDROID_SOCKET_NAMESPACE_RESERVED, 1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SOCK_DGRAM) < 0) { 1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unlink(ctrl->local.sun_path); 1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ctrl; 1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* ANDROID */ 1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->dest.sun_family = AF_UNIX; 195b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (os_strncmp(ctrl_path, "@abstract:", 10) == 0) { 196b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt ctrl->dest.sun_path[0] = '\0'; 197b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt os_strlcpy(ctrl->dest.sun_path + 1, ctrl_path + 10, 198b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt sizeof(ctrl->dest.sun_path) - 1); 199b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt } else { 200b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt res = os_strlcpy(ctrl->dest.sun_path, ctrl_path, 201b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt sizeof(ctrl->dest.sun_path)); 202b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt if (res >= sizeof(ctrl->dest.sun_path)) { 203b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt close(ctrl->s); 204b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt os_free(ctrl); 205b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt return NULL; 206b6e9aaf735990dc64cdb6efccc03d076768eabf3Dmitry Shmidt } 2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, 2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt sizeof(ctrl->dest)) < 0) { 2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unlink(ctrl->local.sun_path); 2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 21661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt /* 21761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * Make socket non-blocking so that we don't hang forever if 21861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * target dies unexpectedly. 21961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt */ 22061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt flags = fcntl(ctrl->s, F_GETFL); 22161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (flags >= 0) { 22261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt flags |= O_NONBLOCK; 22361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (fcntl(ctrl->s, F_SETFL, flags) < 0) { 22461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt perror("fcntl(ctrl->s, O_NONBLOCK)"); 22561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt /* Not fatal, continue on.*/ 22661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 22761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 22861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ctrl; 2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl == NULL) 2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return; 2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unlink(ctrl->local.sun_path); 2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->s >= 0) 2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID 2451f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/** 2461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that 2471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * may be left over from clients that were previously connected to 2481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * wpa_supplicant. This keeps these files from being orphaned in the 2491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * event of crashes that prevented them from being removed as part 2501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * of the normal orderly shutdown. 2511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */ 2521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtvoid wpa_ctrl_cleanup(void) 2531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{ 2541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt DIR *dir; 2551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt struct dirent entry; 2561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt struct dirent *result; 2571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt size_t dirnamelen; 2581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt size_t maxcopy; 2591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt char pathname[PATH_MAX]; 2601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt char *namep; 2611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL) 2631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt return; 2641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/", 2661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt CONFIG_CTRL_IFACE_CLIENT_DIR); 2671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt if (dirnamelen >= sizeof(pathname)) { 2681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt closedir(dir); 2691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt return; 2701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt } 2711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt namep = pathname + dirnamelen; 2721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt maxcopy = PATH_MAX - dirnamelen; 2731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt while (readdir_r(dir, &entry, &result) == 0 && result != NULL) { 2743733ef3f86d438d165bb13eb3b41415c3410be85Dmitry Shmidt if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy) 2753733ef3f86d438d165bb13eb3b41415c3410be85Dmitry Shmidt unlink(pathname); 2761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt } 2771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt closedir(dir); 2781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt} 2791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */ 2801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#else /* CONFIG_CTRL_IFACE_UNIX */ 2821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#ifdef ANDROID 2841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtvoid wpa_ctrl_cleanup(void) 2851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{ 2861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt} 2871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#endif /* ANDROID */ 2881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt 2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UNIX */ 2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP 2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct wpa_ctrl *ctrl; 2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char buf[128]; 2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t len; 29961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 30061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt struct hostent *h; 30161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 303fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt ctrl = os_zalloc(sizeof(*ctrl)); 3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl == NULL) 3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 307f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 308f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->s = socket(PF_INET6, SOCK_DGRAM, 0); 309f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->s = socket(PF_INET, SOCK_DGRAM, 0); 311f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->s < 0) { 3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt perror("socket"); 3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 318f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 319f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->local.sin6_family = AF_INET6; 320f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 321f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->local.sin6_addr = in6addr_any; 322f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 323f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt inet_pton(AF_INET6, "::1", &ctrl->local.sin6_addr); 324f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 325f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->local.sin_family = AF_INET; 32761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 32861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->local.sin_addr.s_addr = INADDR_ANY; 32961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1); 33161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 332f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 333f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt 3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (bind(ctrl->s, (struct sockaddr *) &ctrl->local, 3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt sizeof(ctrl->local)) < 0) { 3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 341f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 342f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->dest.sin6_family = AF_INET6; 343f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt inet_pton(AF_INET6, "::1", &ctrl->dest.sin6_addr); 344f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->dest.sin6_port = htons(WPA_CTRL_IFACE_PORT); 345f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->dest.sin_family = AF_INET; 3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1); 3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT); 349f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 35061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 35161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE 35261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (ctrl_path) { 35361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt char *port, *name; 35461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt int port_id; 355f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 356f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt char *scope; 357f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt int scope_id = 0; 358f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 35961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 36061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt name = os_strdup(ctrl_path); 36161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (name == NULL) { 36261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt close(ctrl->s); 36361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl); 36461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt return NULL; 36561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 366f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 367f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt port = os_strchr(name, ','); 368f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 36961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt port = os_strchr(name, ':'); 370f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 37161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 37261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (port) { 37361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt port_id = atoi(&port[1]); 37461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt port[0] = '\0'; 37561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } else 37661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt port_id = WPA_CTRL_IFACE_PORT; 37761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 378f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 379f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt scope = os_strchr(name, '%'); 380f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt if (scope) { 381f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt scope_id = if_nametoindex(&scope[1]); 382f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt scope[0] = '\0'; 383f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt } 384f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt h = gethostbyname2(name, AF_INET6); 385f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 38661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt h = gethostbyname(name); 387f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 38861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->remote_ip = os_strdup(name); 38961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(name); 39061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (h == NULL) { 39161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt perror("gethostbyname"); 39261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt close(ctrl->s); 39361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl->remote_ip); 39461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl); 39561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt return NULL; 39661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 397f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 398f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->dest.sin6_scope_id = scope_id; 399f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ctrl->dest.sin6_port = htons(port_id); 400f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt os_memcpy(&ctrl->dest.sin6_addr, h->h_addr, h->h_length); 401f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 40261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->dest.sin_port = htons(port_id); 403f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt os_memcpy(&ctrl->dest.sin_addr.s_addr, h->h_addr, h->h_length); 404f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 40561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } else 40661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->remote_ip = os_strdup("localhost"); 40761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ 40861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest, 4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt sizeof(ctrl->dest)) < 0) { 411f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP_IPV6 412f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt char addr[INET6_ADDRSTRLEN]; 413f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s", 414f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt inet_ntop(AF_INET6, &ctrl->dest.sin6_addr, addr, 415f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt sizeof(ctrl->dest)), 416f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ntohs(ctrl->dest.sin6_port), 417f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt strerror(errno)); 418f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 419f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s", 420f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt inet_ntoa(ctrl->dest.sin_addr), 421f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt ntohs(ctrl->dest.sin_port), 422f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt strerror(errno)); 423f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ 4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 42561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl->remote_ip); 4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt len = sizeof(buf) - 1; 4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) { 4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt buf[len] = '\0'; 4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->cookie = os_strdup(buf); 4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 43661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (wpa_ctrl_request(ctrl, "IFNAME", 6, buf, &len, NULL) == 0) { 43761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt buf[len] = '\0'; 43861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->remote_ifname = os_strdup(buf); 43961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 44061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 4418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ctrl; 4428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 4438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 44561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtchar * wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl) 44661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{ 44761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#define WPA_CTRL_MAX_PS_NAME 100 44861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt static char ps[WPA_CTRL_MAX_PS_NAME] = {}; 44961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_snprintf(ps, WPA_CTRL_MAX_PS_NAME, "%s/%s", 45061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt ctrl->remote_ip, ctrl->remote_ifname); 45161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt return ps; 45261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt} 45361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 45461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt 4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt close(ctrl->s); 4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl->cookie); 45961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl->remote_ifname); 46061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_free(ctrl->remote_ip); 4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP */ 4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CTRL_IFACE_SOCKET 4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, 4698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char *reply, size_t *reply_len, 4708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void (*msg_cb)(char *msg, size_t len)) 4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct timeval tv; 473fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt struct os_reltime started_at; 4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int res; 4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fd_set rfds; 4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const char *_cmd; 4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char *cmd_buf = NULL; 4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t _cmd_len; 4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_UDP 4818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->cookie) { 4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char *pos; 4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len; 4848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt cmd_buf = os_malloc(_cmd_len); 4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (cmd_buf == NULL) 4868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _cmd = cmd_buf; 4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pos = cmd_buf; 4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_strlcpy(pos, ctrl->cookie, _cmd_len); 4908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pos += os_strlen(ctrl->cookie); 4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *pos++ = ' '; 4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(pos, cmd, cmd_len); 4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else 4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_UDP */ 4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt { 4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _cmd = cmd; 4978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _cmd_len = cmd_len; 4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 50061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt errno = 0; 50161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt started_at.sec = 0; 50261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt started_at.usec = 0; 50361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtretry_send: 5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) { 50561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK) 50661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt { 50761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt /* 50861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * Must be a non-blocking socket... Try for a bit 50961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * longer before giving up. 51061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt */ 51161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt if (started_at.sec == 0) 512fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt os_get_reltime(&started_at); 51361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt else { 514fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt struct os_reltime n; 515fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt os_get_reltime(&n); 51661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt /* Try for a few seconds. */ 517fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt if (os_reltime_expired(&n, &started_at, 5)) 51861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt goto send_err; 51961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 52061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt os_sleep(1, 0); 52161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt goto retry_send; 52261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt } 52361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt send_err: 5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(cmd_buf); 5258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(cmd_buf); 5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (;;) { 5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt tv.tv_sec = 10; 5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt tv.tv_usec = 0; 5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt FD_ZERO(&rfds); 5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt FD_SET(ctrl->s, &rfds); 5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); 535b97e428f8acf1ecb93f38f8d0063d2f2fd0bc36eDmitry Shmidt if (res < 0 && errno == EINTR) 536b97e428f8acf1ecb93f38f8d0063d2f2fd0bc36eDmitry Shmidt continue; 5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (res < 0) 5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return res; 5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (FD_ISSET(ctrl->s, &rfds)) { 5408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt res = recv(ctrl->s, reply, *reply_len, 0); 5418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (res < 0) 5428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return res; 5438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (res > 0 && reply[0] == '<') { 5448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* This is an unsolicited message from 5458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wpa_supplicant, not the reply to the 5468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * request. Use msg_cb to report this to the 5478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * caller. */ 5488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (msg_cb) { 5498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Make sure the message is nul 5508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * terminated. */ 5518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if ((size_t) res == *reply_len) 5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt res = (*reply_len) - 1; 5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt reply[res] = '\0'; 5548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt msg_cb(reply, res); 5558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt continue; 5578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *reply_len = res; 5598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 5608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 5618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -2; 5628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 5658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CTRL_IFACE_SOCKET */ 5678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach) 5708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 5718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char buf[10]; 5728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int ret; 5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t len = 10; 5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6, 5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt buf, &len, NULL); 5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ret < 0) 5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ret; 5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0) 5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_attach(struct wpa_ctrl *ctrl) 5868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 5878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return wpa_ctrl_attach_helper(ctrl, 1); 5888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_detach(struct wpa_ctrl *ctrl) 5928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 5938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return wpa_ctrl_attach_helper(ctrl, 0); 5948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CTRL_IFACE_SOCKET 5988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) 6008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 6018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int res; 6028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt res = recv(ctrl->s, reply, *reply_len, 0); 6048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (res < 0) 6058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return res; 6068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *reply_len = res; 6078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 6088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 6098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_pending(struct wpa_ctrl *ctrl) 6128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 6138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct timeval tv; 6148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fd_set rfds; 6158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt tv.tv_sec = 0; 6168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt tv.tv_usec = 0; 6178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt FD_ZERO(&rfds); 6188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt FD_SET(ctrl->s, &rfds); 6198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt select(ctrl->s + 1, &rfds, NULL, NULL, &tv); 6208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return FD_ISSET(ctrl->s, &rfds); 6218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 6228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) 6258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 6268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ctrl->s; 6278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 6288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CTRL_IFACE_SOCKET */ 6308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE 6338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef WPA_SUPPLICANT_NAMED_PIPE 6358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant" 6368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif 6378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE) 6388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) 6408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 6418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct wpa_ctrl *ctrl; 6428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD mode; 6438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt TCHAR name[256]; 6448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int i, ret; 6458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl = os_malloc(sizeof(*ctrl)); 6478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl == NULL) 6488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 6498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memset(ctrl, 0, sizeof(*ctrl)); 6508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef UNICODE 6528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl_path == NULL) 6538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX); 6548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt else 6558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"), 6568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl_path); 6578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else /* UNICODE */ 6588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl_path == NULL) 6598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX); 6608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt else 6618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s", 6628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl_path); 6638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* UNICODE */ 664fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt if (os_snprintf_error(256, ret)) { 6658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 6668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 6678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 6688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (i = 0; i < 10; i++) { 6708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, 6718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt NULL, OPEN_EXISTING, 0, NULL); 6728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* 6738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Current named pipe server side in wpa_supplicant is 6748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * re-opening the pipe for new clients only after the previous 6758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * one is taken into use. This leaves a small window for race 6768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * conditions when two connections are being opened at almost 6778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the same time. Retry if that was the case. 6788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 6798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->pipe != INVALID_HANDLE_VALUE || 6808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt GetLastError() != ERROR_PIPE_BUSY) 6818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 6828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt WaitNamedPipe(name, 1000); 6838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 6848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (ctrl->pipe == INVALID_HANDLE_VALUE) { 6858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 6868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 6878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 6888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt mode = PIPE_READMODE_MESSAGE; 6908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) { 6918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(ctrl->pipe); 6928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 6938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 6948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 6958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ctrl; 6978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 6988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 6998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wpa_ctrl_close(struct wpa_ctrl *ctrl) 7018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 7028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(ctrl->pipe); 7038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(ctrl); 7048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 7058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, 7088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char *reply, size_t *reply_len, 7098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void (*msg_cb)(char *msg, size_t len)) 7108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 7118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD written; 7128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD readlen = *reply_len; 7138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL)) 7158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 7168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL)) 7188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 7198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *reply_len = readlen; 7208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 7228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 7238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len) 7268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 7278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD len = *reply_len; 7288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL)) 7298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 7308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *reply_len = len; 7318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 7328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 7338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_pending(struct wpa_ctrl *ctrl) 7368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 7378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD left; 7388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL)) 7408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 7418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return left ? 1 : 0; 7428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 7438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wpa_ctrl_get_fd(struct wpa_ctrl *ctrl) 7468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 7478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 7488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 7498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ 7518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 7528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_CTRL_IFACE */ 753