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