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