]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface_unix.c
tests: wpa_supplicant control socket and event burst
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface_unix.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / UNIX domain socket -based control interface
c9b55597 3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10#include <sys/un.h>
11#include <sys/stat.h>
12#include <grp.h>
19b9436c 13#include <stddef.h>
4fdc8def
BG
14#include <unistd.h>
15#include <fcntl.h>
2e95cfc1
JM
16#ifdef __linux__
17#include <sys/ioctl.h>
18#include <linux/sockios.h>
19#endif /* __linux__ */
b3f3865e
DS
20#ifdef ANDROID
21#include <cutils/sockets.h>
22#endif /* ANDROID */
6fc6879b 23
09e47a07
JM
24#include "utils/common.h"
25#include "utils/eloop.h"
26#include "utils/list.h"
6fc6879b 27#include "eapol_supp/eapol_supp_sm.h"
09e47a07 28#include "config.h"
6fc6879b
JM
29#include "wpa_supplicant_i.h"
30#include "ctrl_iface.h"
31
32/* Per-interface ctrl_iface */
33
34/**
35 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
36 *
37 * This structure is used to store information about registered control
38 * interface monitors into struct wpa_supplicant. This data is private to
39 * ctrl_iface_unix.c and should not be touched directly from other files.
40 */
41struct wpa_ctrl_dst {
09e47a07 42 struct dl_list list;
6fc6879b
JM
43 struct sockaddr_un addr;
44 socklen_t addrlen;
45 int debug_level;
46 int errors;
47};
48
49
50struct ctrl_iface_priv {
51 struct wpa_supplicant *wpa_s;
52 int sock;
09e47a07 53 struct dl_list ctrl_dst;
3a7414b6 54 int android_control_socket;
3fdaaa8f
JM
55 struct dl_list msg_queue;
56 unsigned int throttle_count;
6fc6879b
JM
57};
58
59
214e428b
JM
60struct ctrl_iface_global_priv {
61 struct wpa_global *global;
62 int sock;
63 struct dl_list ctrl_dst;
3a7414b6 64 int android_control_socket;
3fdaaa8f
JM
65 struct dl_list msg_queue;
66 unsigned int throttle_count;
67};
68
69struct ctrl_iface_msg {
70 struct dl_list list;
71 struct wpa_supplicant *wpa_s;
72 int level;
73 enum wpa_msg_type type;
74 const char *txt;
75 size_t len;
214e428b
JM
76};
77
78
89286e91
JM
79static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
80 const char *ifname, int sock,
214e428b 81 struct dl_list *ctrl_dst,
6fc6879b 82 int level, const char *buf,
89286e91
JM
83 size_t len,
84 struct ctrl_iface_priv *priv,
85 struct ctrl_iface_global_priv *gp);
86static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
87 struct ctrl_iface_priv *priv);
88static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
89 struct ctrl_iface_global_priv *priv);
6fc6879b
JM
90
91
2e95cfc1
JM
92static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
93 size_t len)
94{
95#ifdef __linux__
96 socklen_t optlen;
97 int sndbuf, outq;
43fa110b 98 int level = MSG_MSGDUMP;
2e95cfc1
JM
99
100 if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0)
101 level = MSG_EXCESSIVE;
102
103 optlen = sizeof(sndbuf);
104 sndbuf = 0;
105 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
106 sndbuf = -1;
107
108 if (ioctl(sock, SIOCOUTQ, &outq) < 0)
109 outq = -1;
110
111 wpa_printf(level,
112 "CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d",
113 title, sock, sndbuf, outq, (int) len);
114#endif /* __linux__ */
115}
116
117
214e428b 118static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
6fc6879b 119 struct sockaddr_un *from,
0efcad2c 120 socklen_t fromlen, int global)
6fc6879b
JM
121{
122 struct wpa_ctrl_dst *dst;
c9b55597 123 char addr_txt[200];
6fc6879b
JM
124
125 dst = os_zalloc(sizeof(*dst));
126 if (dst == NULL)
127 return -1;
128 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
129 dst->addrlen = fromlen;
130 dst->debug_level = MSG_INFO;
214e428b 131 dl_list_add(ctrl_dst, &dst->list);
c9b55597
JM
132 printf_encode(addr_txt, sizeof(addr_txt),
133 (u8 *) from->sun_path,
134 fromlen - offsetof(struct sockaddr_un, sun_path));
0efcad2c
JM
135 wpa_printf(MSG_DEBUG, "CTRL_IFACE %smonitor attached %s",
136 global ? "global " : "", addr_txt);
6fc6879b
JM
137 return 0;
138}
139
140
214e428b 141static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
6fc6879b
JM
142 struct sockaddr_un *from,
143 socklen_t fromlen)
144{
09e47a07 145 struct wpa_ctrl_dst *dst;
6fc6879b 146
214e428b 147 dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
6fc6879b
JM
148 if (fromlen == dst->addrlen &&
149 os_memcmp(from->sun_path, dst->addr.sun_path,
19b9436c
SL
150 fromlen - offsetof(struct sockaddr_un, sun_path))
151 == 0) {
c9b55597
JM
152 char addr_txt[200];
153 printf_encode(addr_txt, sizeof(addr_txt),
154 (u8 *) from->sun_path,
155 fromlen -
156 offsetof(struct sockaddr_un, sun_path));
157 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
158 addr_txt);
a235aca3
JM
159 dl_list_del(&dst->list);
160 os_free(dst);
6fc6879b
JM
161 return 0;
162 }
6fc6879b
JM
163 }
164 return -1;
165}
166
167
168static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
169 struct sockaddr_un *from,
170 socklen_t fromlen,
171 char *level)
172{
173 struct wpa_ctrl_dst *dst;
174
175 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
176
09e47a07 177 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
6fc6879b
JM
178 if (fromlen == dst->addrlen &&
179 os_memcmp(from->sun_path, dst->addr.sun_path,
19b9436c
SL
180 fromlen - offsetof(struct sockaddr_un, sun_path))
181 == 0) {
c9b55597 182 char addr_txt[200];
6fc6879b 183 dst->debug_level = atoi(level);
c9b55597
JM
184 printf_encode(addr_txt, sizeof(addr_txt),
185 (u8 *) from->sun_path, fromlen -
186 offsetof(struct sockaddr_un, sun_path));
187 wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
188 dst->debug_level, addr_txt);
6fc6879b
JM
189 return 0;
190 }
6fc6879b
JM
191 }
192
193 return -1;
194}
195
196
197static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
198 void *sock_ctx)
199{
200 struct wpa_supplicant *wpa_s = eloop_ctx;
201 struct ctrl_iface_priv *priv = sock_ctx;
b563b388 202 char buf[4096];
6fc6879b
JM
203 int res;
204 struct sockaddr_un from;
205 socklen_t fromlen = sizeof(from);
742e715b 206 char *reply = NULL, *reply_buf = NULL;
6fc6879b
JM
207 size_t reply_len = 0;
208 int new_attached = 0;
209
210 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
211 (struct sockaddr *) &from, &fromlen);
212 if (res < 0) {
2c6f8cf6
JM
213 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
214 strerror(errno));
6fc6879b
JM
215 return;
216 }
217 buf[res] = '\0';
218
219 if (os_strcmp(buf, "ATTACH") == 0) {
214e428b 220 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
0efcad2c 221 fromlen, 0))
6fc6879b
JM
222 reply_len = 1;
223 else {
224 new_attached = 1;
225 reply_len = 2;
226 }
227 } else if (os_strcmp(buf, "DETACH") == 0) {
214e428b
JM
228 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
229 fromlen))
6fc6879b
JM
230 reply_len = 1;
231 else
232 reply_len = 2;
233 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
234 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
235 buf + 6))
236 reply_len = 1;
237 else
238 reply_len = 2;
239 } else {
742e715b
JM
240 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
241 &reply_len);
242 reply = reply_buf;
14fd0331
JM
243
244 /*
245 * There could be some password/key material in the command, so
246 * clear the buffer explicitly now that it is not needed
247 * anymore.
248 */
249 os_memset(buf, 0, res);
742e715b
JM
250 }
251
252 if (!reply && reply_len == 1) {
253 reply = "FAIL\n";
254 reply_len = 5;
255 } else if (!reply && reply_len == 2) {
256 reply = "OK\n";
257 reply_len = 3;
6fc6879b
JM
258 }
259
260 if (reply) {
2e95cfc1
JM
261 wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply,
262 reply_len);
79986bf6
JM
263 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
264 fromlen) < 0) {
89286e91 265 int _errno = errno;
79986bf6 266 wpa_dbg(wpa_s, MSG_DEBUG,
89286e91
JM
267 "ctrl_iface sendto failed: %d - %s",
268 _errno, strerror(_errno));
269 if (_errno == ENOBUFS || _errno == EAGAIN) {
270 /*
271 * The socket send buffer could be full. This
272 * may happen if client programs are not
273 * receiving their pending messages. Close and
274 * reopen the socket as a workaround to avoid
275 * getting stuck being unable to send any new
276 * responses.
277 */
278 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
279 if (sock < 0) {
280 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
281 }
282 }
283 if (new_attached) {
284 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
285 new_attached = 0;
286 wpa_supplicant_ctrl_iface_detach(
287 &priv->ctrl_dst, &from, fromlen);
288 }
79986bf6 289 }
6fc6879b 290 }
742e715b 291 os_free(reply_buf);
6fc6879b
JM
292
293 if (new_attached)
294 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
295}
296
297
298static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
299{
300 char *buf;
301 size_t len;
6ed626df 302 char *pbuf, *dir = NULL;
6fc6879b
JM
303 int res;
304
305 if (wpa_s->conf->ctrl_interface == NULL)
306 return NULL;
307
308 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
309 if (pbuf == NULL)
310 return NULL;
311 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
6ed626df 312 char *gid_str;
6fc6879b
JM
313 dir = pbuf + 4;
314 gid_str = os_strstr(dir, " GROUP=");
6ed626df 315 if (gid_str)
6fc6879b 316 *gid_str = '\0';
6fc6879b
JM
317 } else
318 dir = pbuf;
319
320 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
321 buf = os_malloc(len);
322 if (buf == NULL) {
323 os_free(pbuf);
324 return NULL;
325 }
326
327 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
d85e1fc8 328 if (os_snprintf_error(len, res)) {
6fc6879b
JM
329 os_free(pbuf);
330 os_free(buf);
331 return NULL;
332 }
333#ifdef __CYGWIN__
334 {
335 /* Windows/WinPcap uses interface names that are not suitable
336 * as a file name - convert invalid chars to underscores */
337 char *pos = buf;
338 while (*pos) {
339 if (*pos == '\\')
340 *pos = '_';
341 pos++;
342 }
343 }
344#endif /* __CYGWIN__ */
345 os_free(pbuf);
346 return buf;
347}
348
349
3fdaaa8f
JM
350static int wpas_ctrl_iface_throttle(int sock)
351{
352#ifdef __linux__
353 socklen_t optlen;
354 int sndbuf, outq;
355
356 optlen = sizeof(sndbuf);
357 sndbuf = 0;
358 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0 ||
359 ioctl(sock, SIOCOUTQ, &outq) < 0 ||
360 sndbuf <= 0 || outq < 0)
361 return 0;
362 return outq > sndbuf / 2;
363#else /* __linux__ */
364 return 0;
365#endif /* __linux__ */
366}
367
368
369static void wpas_ctrl_msg_send_pending_global(struct wpa_global *global)
370{
371 struct ctrl_iface_global_priv *gpriv;
372 struct ctrl_iface_msg *msg;
373
374 gpriv = global->ctrl_iface;
375 while (gpriv && !dl_list_empty(&gpriv->msg_queue) &&
376 !wpas_ctrl_iface_throttle(gpriv->sock)) {
377 msg = dl_list_first(&gpriv->msg_queue, struct ctrl_iface_msg,
378 list);
379 if (!msg)
380 break;
381 dl_list_del(&msg->list);
382 wpa_supplicant_ctrl_iface_send(
383 msg->wpa_s,
384 msg->type != WPA_MSG_PER_INTERFACE ?
385 NULL : msg->wpa_s->ifname,
386 gpriv->sock, &gpriv->ctrl_dst, msg->level,
387 msg->txt, msg->len, NULL, gpriv);
388 os_free(msg);
389 }
390}
391
392
393static void wpas_ctrl_msg_send_pending_iface(struct wpa_supplicant *wpa_s)
394{
395 struct ctrl_iface_priv *priv;
396 struct ctrl_iface_msg *msg;
397
398 priv = wpa_s->ctrl_iface;
399 while (priv && !dl_list_empty(&priv->msg_queue) &&
400 !wpas_ctrl_iface_throttle(priv->sock)) {
401 msg = dl_list_first(&priv->msg_queue, struct ctrl_iface_msg,
402 list);
403 if (!msg)
404 break;
405 dl_list_del(&msg->list);
406 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
407 &priv->ctrl_dst, msg->level,
408 msg->txt, msg->len, priv, NULL);
409 os_free(msg);
410 }
411}
412
413
414static void wpas_ctrl_msg_queue_timeout(void *eloop_ctx, void *timeout_ctx)
415{
416 struct wpa_supplicant *wpa_s = eloop_ctx;
417 struct ctrl_iface_priv *priv;
418 struct ctrl_iface_global_priv *gpriv;
419 int sock = -1, gsock = -1;
420
421 wpas_ctrl_msg_send_pending_global(wpa_s->global);
422 wpas_ctrl_msg_send_pending_iface(wpa_s);
423
424 priv = wpa_s->ctrl_iface;
425 if (priv && !dl_list_empty(&priv->msg_queue))
426 sock = priv->sock;
427
428 gpriv = wpa_s->global->ctrl_iface;
429 if (gpriv && !dl_list_empty(&gpriv->msg_queue))
430 gsock = gpriv->sock;
431
432 if (sock > -1 || gsock > -1) {
433 /* Continue pending message transmission from a timeout */
434 wpa_printf(MSG_MSGDUMP,
435 "CTRL: Had to throttle pending event message transmission for (sock %d gsock %d)",
436 sock, gsock);
437 eloop_register_timeout(0, 20000, wpas_ctrl_msg_queue_timeout,
438 wpa_s, NULL);
439 }
440}
441
442
443static void wpas_ctrl_msg_queue(struct dl_list *queue,
444 struct wpa_supplicant *wpa_s, int level,
445 enum wpa_msg_type type,
446 const char *txt, size_t len)
447{
448 struct ctrl_iface_msg *msg;
449
450 msg = os_zalloc(sizeof(*msg) + len);
451 if (!msg)
452 return;
453
454 msg->wpa_s = wpa_s;
455 msg->level = level;
456 msg->type = type;
457 os_memcpy(msg + 1, txt, len);
458 msg->txt = (const char *) (msg + 1);
459 msg->len = len;
460 dl_list_add_tail(queue, &msg->list);
461 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
462 eloop_register_timeout(0, 0, wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
463}
464
465
466static void wpas_ctrl_msg_queue_limit(unsigned int throttle_count,
467 struct dl_list *queue)
468{
469 struct ctrl_iface_msg *msg;
470
471 if (throttle_count < 2000)
472 return;
473
474 msg = dl_list_first(queue, struct ctrl_iface_msg, list);
475 if (msg) {
476 wpa_printf(MSG_DEBUG, "CTRL: Dropped oldest pending message");
477 dl_list_del(&msg->list);
478 os_free(msg);
479 }
480}
481
482
995a3a06
JM
483static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
484 enum wpa_msg_type type,
6fc6879b
JM
485 const char *txt, size_t len)
486{
487 struct wpa_supplicant *wpa_s = ctx;
3fdaaa8f
JM
488 struct ctrl_iface_priv *priv;
489 struct ctrl_iface_global_priv *gpriv;
214e428b
JM
490
491 if (wpa_s == NULL)
6fc6879b 492 return;
214e428b 493
3fdaaa8f
JM
494 gpriv = wpa_s->global->ctrl_iface;
495
496 if (type != WPA_MSG_NO_GLOBAL && gpriv &&
497 !dl_list_empty(&gpriv->ctrl_dst)) {
498 if (!dl_list_empty(&gpriv->msg_queue) ||
499 wpas_ctrl_iface_throttle(gpriv->sock)) {
500 if (gpriv->throttle_count == 0) {
501 wpa_printf(MSG_MSGDUMP,
502 "CTRL: Had to throttle global event message for sock %d",
503 gpriv->sock);
504 }
505 gpriv->throttle_count++;
506 wpas_ctrl_msg_queue_limit(gpriv->throttle_count,
507 &gpriv->msg_queue);
508 wpas_ctrl_msg_queue(&gpriv->msg_queue, wpa_s, level,
509 type, txt, len);
510 } else {
511 if (gpriv->throttle_count) {
512 wpa_printf(MSG_MSGDUMP,
513 "CTRL: Had to throttle %u global event message(s) for sock %d",
514 gpriv->throttle_count, gpriv->sock);
515 }
516 gpriv->throttle_count = 0;
995a3a06
JM
517 wpa_supplicant_ctrl_iface_send(
518 wpa_s,
ee1e3f57
AN
519 type != WPA_MSG_PER_INTERFACE ?
520 NULL : wpa_s->ifname,
3fdaaa8f
JM
521 gpriv->sock, &gpriv->ctrl_dst, level,
522 txt, len, NULL, gpriv);
214e428b
JM
523 }
524 }
525
3fdaaa8f
JM
526 priv = wpa_s->ctrl_iface;
527
528 if (type != WPA_MSG_ONLY_GLOBAL && priv) {
529 if (!dl_list_empty(&priv->msg_queue) ||
530 wpas_ctrl_iface_throttle(priv->sock)) {
531 if (priv->throttle_count == 0) {
532 wpa_printf(MSG_MSGDUMP,
533 "CTRL: Had to throttle event message for sock %d",
534 priv->sock);
535 }
536 priv->throttle_count++;
537 wpas_ctrl_msg_queue_limit(priv->throttle_count,
538 &priv->msg_queue);
539 wpas_ctrl_msg_queue(&priv->msg_queue, wpa_s, level,
540 type, txt, len);
541 } else {
542 if (priv->throttle_count) {
543 wpa_printf(MSG_MSGDUMP,
544 "CTRL: Had to throttle %u event message(s) for sock %d",
545 priv->throttle_count, priv->sock);
546 }
547 priv->throttle_count = 0;
548 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
549 &priv->ctrl_dst, level,
550 txt, len, priv, NULL);
551 }
552 }
6fc6879b
JM
553}
554
555
89286e91
JM
556static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
557 struct ctrl_iface_priv *priv)
6fc6879b 558{
6fc6879b
JM
559 struct sockaddr_un addr;
560 char *fname = NULL;
561 gid_t gid = 0;
562 int gid_set = 0;
563 char *buf, *dir = NULL, *gid_str = NULL;
564 struct group *grp;
565 char *endp;
4fdc8def 566 int flags;
6fc6879b 567
6fc6879b
JM
568 buf = os_strdup(wpa_s->conf->ctrl_interface);
569 if (buf == NULL)
570 goto fail;
b3f3865e
DS
571#ifdef ANDROID
572 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
573 wpa_s->conf->ctrl_interface);
574 priv->sock = android_get_control_socket(addr.sun_path);
3a7414b6
NM
575 if (priv->sock >= 0) {
576 priv->android_control_socket = 1;
b3f3865e 577 goto havesock;
3a7414b6 578 }
b3f3865e 579#endif /* ANDROID */
6fc6879b
JM
580 if (os_strncmp(buf, "DIR=", 4) == 0) {
581 dir = buf + 4;
582 gid_str = os_strstr(dir, " GROUP=");
583 if (gid_str) {
584 *gid_str = '\0';
585 gid_str += 7;
586 }
587 } else {
588 dir = buf;
589 gid_str = wpa_s->conf->ctrl_interface_group;
590 }
591
592 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
593 if (errno == EEXIST) {
594 wpa_printf(MSG_DEBUG, "Using existing control "
595 "interface directory.");
596 } else {
2c6f8cf6
JM
597 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
598 dir, strerror(errno));
6fc6879b
JM
599 goto fail;
600 }
601 }
602
d49ea682
JM
603#ifdef ANDROID
604 /*
605 * wpa_supplicant is started from /init.*.rc on Android and that seems
606 * to be using umask 0077 which would leave the control interface
607 * directory without group access. This breaks things since Wi-Fi
608 * framework assumes that this directory can be accessed by other
609 * applications in the wifi group. Fix this by adding group access even
610 * if umask value would prevent this.
611 */
612 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
613 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
614 strerror(errno));
615 /* Try to continue anyway */
616 }
617#endif /* ANDROID */
618
6fc6879b
JM
619 if (gid_str) {
620 grp = getgrnam(gid_str);
621 if (grp) {
622 gid = grp->gr_gid;
623 gid_set = 1;
624 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
625 " (from group name '%s')",
626 (int) gid, gid_str);
627 } else {
628 /* Group name not found - try to parse this as gid */
629 gid = strtol(gid_str, &endp, 10);
630 if (*gid_str == '\0' || *endp != '\0') {
8e888179 631 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
6fc6879b
JM
632 "'%s'", gid_str);
633 goto fail;
634 }
635 gid_set = 1;
636 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
637 (int) gid);
638 }
639 }
640
641 if (gid_set && chown(dir, -1, gid) < 0) {
2c6f8cf6
JM
642 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
643 dir, (int) gid, strerror(errno));
6fc6879b
JM
644 goto fail;
645 }
646
3fd2a226
AAS
647 /* Make sure the group can enter and read the directory */
648 if (gid_set &&
649 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
650 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
651 strerror(errno));
652 goto fail;
653 }
654
6fc6879b
JM
655 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
656 sizeof(addr.sun_path)) {
657 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
658 goto fail;
659 }
660
661 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
662 if (priv->sock < 0) {
2c6f8cf6 663 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
6fc6879b
JM
664 goto fail;
665 }
666
667 os_memset(&addr, 0, sizeof(addr));
09bd6e8c 668#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
19b9436c
SL
669 addr.sun_len = sizeof(addr);
670#endif /* __FreeBSD__ */
6fc6879b
JM
671 addr.sun_family = AF_UNIX;
672 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
673 if (fname == NULL)
674 goto fail;
675 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
676 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
677 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
678 strerror(errno));
679 if (connect(priv->sock, (struct sockaddr *) &addr,
680 sizeof(addr)) < 0) {
681 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
682 " allow connections - assuming it was left"
683 "over from forced program termination");
684 if (unlink(fname) < 0) {
2c6f8cf6
JM
685 wpa_printf(MSG_ERROR,
686 "Could not unlink existing ctrl_iface socket '%s': %s",
687 fname, strerror(errno));
6fc6879b
JM
688 goto fail;
689 }
690 if (bind(priv->sock, (struct sockaddr *) &addr,
691 sizeof(addr)) < 0) {
2c6f8cf6
JM
692 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
693 strerror(errno));
6fc6879b
JM
694 goto fail;
695 }
696 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
697 "ctrl_iface socket '%s'", fname);
698 } else {
699 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
700 "be in use - cannot override it");
701 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
702 "not used anymore", fname);
703 os_free(fname);
704 fname = NULL;
705 goto fail;
706 }
707 }
708
709 if (gid_set && chown(fname, -1, gid) < 0) {
2c6f8cf6
JM
710 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
711 fname, (int) gid, strerror(errno));
6fc6879b
JM
712 goto fail;
713 }
714
715 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2c6f8cf6
JM
716 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
717 fname, strerror(errno));
6fc6879b
JM
718 goto fail;
719 }
720 os_free(fname);
721
b3f3865e
DS
722#ifdef ANDROID
723havesock:
724#endif /* ANDROID */
4fdc8def
BG
725
726 /*
727 * Make socket non-blocking so that we don't hang forever if
728 * target dies unexpectedly.
729 */
730 flags = fcntl(priv->sock, F_GETFL);
731 if (flags >= 0) {
732 flags |= O_NONBLOCK;
733 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
2c6f8cf6
JM
734 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
735 strerror(errno));
4fdc8def
BG
736 /* Not fatal, continue on.*/
737 }
738 }
739
6fc6879b
JM
740 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
741 wpa_s, priv);
742 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
743
744 os_free(buf);
89286e91 745 return 0;
6fc6879b
JM
746
747fail:
89286e91 748 if (priv->sock >= 0) {
6fc6879b 749 close(priv->sock);
89286e91
JM
750 priv->sock = -1;
751 }
6fc6879b
JM
752 if (fname) {
753 unlink(fname);
754 os_free(fname);
755 }
756 os_free(buf);
89286e91
JM
757 return -1;
758}
759
760
761struct ctrl_iface_priv *
762wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
763{
764 struct ctrl_iface_priv *priv;
765
766 priv = os_zalloc(sizeof(*priv));
767 if (priv == NULL)
768 return NULL;
769 dl_list_init(&priv->ctrl_dst);
3fdaaa8f 770 dl_list_init(&priv->msg_queue);
89286e91
JM
771 priv->wpa_s = wpa_s;
772 priv->sock = -1;
773
774 if (wpa_s->conf->ctrl_interface == NULL)
775 return priv;
776
c9cfa6a9
SD
777#ifdef ANDROID
778 if (wpa_s->global->params.ctrl_interface) {
779 int same = 0;
780
781 if (wpa_s->global->params.ctrl_interface[0] == '/') {
782 if (os_strcmp(wpa_s->global->params.ctrl_interface,
783 wpa_s->conf->ctrl_interface) == 0)
784 same = 1;
785 } else if (os_strncmp(wpa_s->global->params.ctrl_interface,
786 "@android:", 9) == 0 ||
787 os_strncmp(wpa_s->global->params.ctrl_interface,
788 "@abstract:", 10) == 0) {
789 char *pos;
790
791 /*
792 * Currently, Android uses @android:wpa_* as the naming
793 * convention for the global ctrl interface. This logic
794 * needs to be revisited if the above naming convention
795 * is modified.
796 */
797 pos = os_strchr(wpa_s->global->params.ctrl_interface,
798 '_');
799 if (pos &&
800 os_strcmp(pos + 1,
801 wpa_s->conf->ctrl_interface) == 0)
802 same = 1;
803 }
804
805 if (same) {
806 /*
807 * The invalid configuration combination might be
808 * possible to hit in an Android OTA upgrade case, so
809 * instead of refusing to start the wpa_supplicant
810 * process, do not open the per-interface ctrl_iface
811 * and continue with the global control interface that
812 * was set from the command line since the Wi-Fi
813 * framework will use it for operations.
814 */
815 wpa_printf(MSG_ERROR,
816 "global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface",
817 wpa_s->global->params.ctrl_interface,
818 wpa_s->conf->ctrl_interface);
819 return priv;
820 }
821 }
822#endif /* ANDROID */
823
89286e91
JM
824 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
825 os_free(priv);
826 return NULL;
827 }
828
829 return priv;
830}
831
832
833static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
834 struct ctrl_iface_priv *priv)
835{
836 int res;
837
838 if (priv->sock <= 0)
839 return -1;
840
3a7414b6
NM
841 /*
842 * On Android, the control socket being used may be the socket
843 * that is created when wpa_supplicant is started as a /init.*.rc
844 * service. Such a socket is maintained as a key-value pair in
845 * Android's environment. Closing this control socket would leave us
846 * in a bad state with an invalid socket descriptor.
847 */
848 if (priv->android_control_socket)
849 return priv->sock;
850
89286e91
JM
851 eloop_unregister_read_sock(priv->sock);
852 close(priv->sock);
853 priv->sock = -1;
854 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
855 if (res < 0)
856 return -1;
857 return priv->sock;
6fc6879b
JM
858}
859
860
861void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
862{
863 struct wpa_ctrl_dst *dst, *prev;
3fdaaa8f
JM
864 struct ctrl_iface_msg *msg, *prev_msg;
865 struct ctrl_iface_global_priv *gpriv;
6fc6879b
JM
866
867 if (priv->sock > -1) {
868 char *fname;
6ed626df 869 char *buf, *dir = NULL;
6fc6879b 870 eloop_unregister_read_sock(priv->sock);
09e47a07 871 if (!dl_list_empty(&priv->ctrl_dst)) {
6fc6879b 872 /*
e0591c3c 873 * Wait before closing the control socket if
6fc6879b
JM
874 * there are any attached monitors in order to allow
875 * them to receive any pending messages.
876 */
877 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
878 "monitors to receive messages");
e0591c3c 879 os_sleep(0, 100000);
6fc6879b
JM
880 }
881 close(priv->sock);
882 priv->sock = -1;
883 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
884 if (fname) {
885 unlink(fname);
886 os_free(fname);
887 }
888
ea61aa1d
JM
889 if (priv->wpa_s->conf->ctrl_interface == NULL)
890 goto free_dst;
6fc6879b
JM
891 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
892 if (buf == NULL)
893 goto free_dst;
894 if (os_strncmp(buf, "DIR=", 4) == 0) {
6ed626df 895 char *gid_str;
6fc6879b
JM
896 dir = buf + 4;
897 gid_str = os_strstr(dir, " GROUP=");
6ed626df 898 if (gid_str)
6fc6879b 899 *gid_str = '\0';
6fc6879b
JM
900 } else
901 dir = buf;
902
903 if (rmdir(dir) < 0) {
904 if (errno == ENOTEMPTY) {
905 wpa_printf(MSG_DEBUG, "Control interface "
906 "directory not empty - leaving it "
907 "behind");
908 } else {
2c6f8cf6
JM
909 wpa_printf(MSG_ERROR,
910 "rmdir[ctrl_interface=%s]: %s",
911 dir, strerror(errno));
6fc6879b
JM
912 }
913 }
914 os_free(buf);
915 }
916
917free_dst:
09e47a07
JM
918 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
919 list)
920 os_free(dst);
3fdaaa8f
JM
921 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
922 struct ctrl_iface_msg, list) {
923 dl_list_del(&msg->list);
924 os_free(msg);
925 }
926 gpriv = priv->wpa_s->global->ctrl_iface;
927 if (gpriv) {
928 dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue,
929 struct ctrl_iface_msg, list) {
930 if (msg->wpa_s == priv->wpa_s) {
931 dl_list_del(&msg->list);
932 os_free(msg);
933 }
934 }
935 }
936 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL);
6fc6879b
JM
937 os_free(priv);
938}
939
940
941/**
942 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
214e428b
JM
943 * @ifname: Interface name for global control socket or %NULL
944 * @sock: Local socket fd
945 * @ctrl_dst: List of attached listeners
6fc6879b
JM
946 * @level: Priority level of the message
947 * @buf: Message data
948 * @len: Message length
949 *
950 * Send a packet to all monitor programs attached to the control interface.
951 */
89286e91
JM
952static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
953 const char *ifname, int sock,
214e428b 954 struct dl_list *ctrl_dst,
6fc6879b 955 int level, const char *buf,
89286e91
JM
956 size_t len,
957 struct ctrl_iface_priv *priv,
958 struct ctrl_iface_global_priv *gp)
6fc6879b
JM
959{
960 struct wpa_ctrl_dst *dst, *next;
961 char levelstr[10];
962 int idx, res;
963 struct msghdr msg;
214e428b 964 struct iovec io[5];
6fc6879b 965
214e428b 966 if (sock < 0 || dl_list_empty(ctrl_dst))
6fc6879b
JM
967 return;
968
969 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
d85e1fc8 970 if (os_snprintf_error(sizeof(levelstr), res))
6fc6879b 971 return;
214e428b
JM
972 idx = 0;
973 if (ifname) {
974 io[idx].iov_base = "IFNAME=";
975 io[idx].iov_len = 7;
976 idx++;
977 io[idx].iov_base = (char *) ifname;
978 io[idx].iov_len = os_strlen(ifname);
979 idx++;
980 io[idx].iov_base = " ";
981 io[idx].iov_len = 1;
982 idx++;
983 }
984 io[idx].iov_base = levelstr;
985 io[idx].iov_len = os_strlen(levelstr);
986 idx++;
987 io[idx].iov_base = (char *) buf;
988 io[idx].iov_len = len;
989 idx++;
6fc6879b
JM
990 os_memset(&msg, 0, sizeof(msg));
991 msg.msg_iov = io;
214e428b 992 msg.msg_iovlen = idx;
6fc6879b 993
214e428b 994 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
89286e91 995 int _errno;
c9b55597 996 char addr_txt[200];
89286e91 997
89286e91
JM
998 if (level < dst->debug_level)
999 continue;
1000
c9b55597
JM
1001 printf_encode(addr_txt, sizeof(addr_txt),
1002 (u8 *) dst->addr.sun_path, dst->addrlen -
1003 offsetof(struct sockaddr_un, sun_path));
89286e91
JM
1004 msg.msg_name = (void *) &dst->addr;
1005 msg.msg_namelen = dst->addrlen;
2e95cfc1 1006 wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
89286e91 1007 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
43fa110b
JM
1008 wpa_printf(MSG_MSGDUMP,
1009 "CTRL_IFACE monitor sent successfully to %s",
c9b55597 1010 addr_txt);
89286e91 1011 dst->errors = 0;
89286e91
JM
1012 continue;
1013 }
1014
1015 _errno = errno;
c9b55597
JM
1016 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
1017 addr_txt, errno, strerror(errno));
89286e91
JM
1018 dst->errors++;
1019
1020 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
c9b55597
JM
1021 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
1022 addr_txt);
89286e91
JM
1023 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
1024 dst->addrlen);
1025 }
1026
1027 if (_errno == ENOBUFS || _errno == EAGAIN) {
1028 /*
1029 * The socket send buffer could be full. This may happen
1030 * if client programs are not receiving their pending
1031 * messages. Close and reopen the socket as a workaround
1032 * to avoid getting stuck being unable to send any new
1033 * responses.
1034 */
1035 if (priv)
1036 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
1037 else if (gp)
1038 sock = wpas_ctrl_iface_global_reinit(
1039 wpa_s->global, gp);
1040 else
1041 break;
1042 if (sock < 0) {
1043 wpa_dbg(wpa_s, MSG_DEBUG,
1044 "Failed to reinitialize ctrl_iface socket");
29179b88 1045 break;
89286e91
JM
1046 }
1047 }
6fc6879b
JM
1048 }
1049}
1050
1051
1052void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
1053{
1054 char buf[256];
1055 int res;
1056 struct sockaddr_un from;
1057 socklen_t fromlen = sizeof(from);
1058
1059 for (;;) {
1060 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
1061 "attach", priv->wpa_s->ifname);
1062 eloop_wait_for_read_sock(priv->sock);
1063
1064 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
1065 (struct sockaddr *) &from, &fromlen);
1066 if (res < 0) {
2c6f8cf6
JM
1067 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1068 strerror(errno));
6fc6879b
JM
1069 continue;
1070 }
1071 buf[res] = '\0';
1072
1073 if (os_strcmp(buf, "ATTACH") == 0) {
1074 /* handle ATTACH signal of first monitor interface */
214e428b 1075 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
0efcad2c
JM
1076 &from, fromlen,
1077 0)) {
79986bf6
JM
1078 if (sendto(priv->sock, "OK\n", 3, 0,
1079 (struct sockaddr *) &from, fromlen) <
1080 0) {
1081 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1082 strerror(errno));
1083 }
6fc6879b
JM
1084 /* OK to continue */
1085 return;
1086 } else {
79986bf6
JM
1087 if (sendto(priv->sock, "FAIL\n", 5, 0,
1088 (struct sockaddr *) &from, fromlen) <
1089 0) {
1090 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1091 strerror(errno));
1092 }
6fc6879b
JM
1093 }
1094 } else {
1095 /* return FAIL for all other signals */
79986bf6
JM
1096 if (sendto(priv->sock, "FAIL\n", 5, 0,
1097 (struct sockaddr *) &from, fromlen) < 0) {
1098 wpa_printf(MSG_DEBUG,
1099 "ctrl_iface sendto failed: %s",
1100 strerror(errno));
1101 }
6fc6879b
JM
1102 }
1103 }
1104}
1105
1106
1107/* Global ctrl_iface */
1108
6fc6879b
JM
1109static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1110 void *sock_ctx)
1111{
1112 struct wpa_global *global = eloop_ctx;
214e428b 1113 struct ctrl_iface_global_priv *priv = sock_ctx;
8615bdfa 1114 char buf[4096];
6fc6879b
JM
1115 int res;
1116 struct sockaddr_un from;
1117 socklen_t fromlen = sizeof(from);
742e715b 1118 char *reply = NULL, *reply_buf = NULL;
6fc6879b
JM
1119 size_t reply_len;
1120
1121 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1122 (struct sockaddr *) &from, &fromlen);
1123 if (res < 0) {
2c6f8cf6
JM
1124 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1125 strerror(errno));
6fc6879b
JM
1126 return;
1127 }
1128 buf[res] = '\0';
1129
214e428b
JM
1130 if (os_strcmp(buf, "ATTACH") == 0) {
1131 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
0efcad2c 1132 fromlen, 1))
214e428b
JM
1133 reply_len = 1;
1134 else
1135 reply_len = 2;
1136 } else if (os_strcmp(buf, "DETACH") == 0) {
1137 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
1138 fromlen))
1139 reply_len = 1;
1140 else
1141 reply_len = 2;
1142 } else {
742e715b
JM
1143 reply_buf = wpa_supplicant_global_ctrl_iface_process(
1144 global, buf, &reply_len);
1145 reply = reply_buf;
14fd0331
JM
1146
1147 /*
1148 * There could be some password/key material in the command, so
1149 * clear the buffer explicitly now that it is not needed
1150 * anymore.
1151 */
1152 os_memset(buf, 0, res);
742e715b
JM
1153 }
1154
1155 if (!reply && reply_len == 1) {
1156 reply = "FAIL\n";
1157 reply_len = 5;
1158 } else if (!reply && reply_len == 2) {
1159 reply = "OK\n";
1160 reply_len = 3;
214e428b 1161 }
6fc6879b
JM
1162
1163 if (reply) {
2e95cfc1
JM
1164 wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
1165 sock, reply, reply_len);
79986bf6
JM
1166 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
1167 fromlen) < 0) {
1168 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1169 strerror(errno));
1170 }
6fc6879b 1171 }
742e715b 1172 os_free(reply_buf);
6fc6879b
JM
1173}
1174
1175
89286e91
JM
1176static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
1177 struct ctrl_iface_global_priv *priv)
6fc6879b 1178{
6fc6879b 1179 struct sockaddr_un addr;
d2a9e2c7 1180 const char *ctrl = global->params.ctrl_interface;
8d6e0350 1181 int flags;
6fc6879b 1182
d2a9e2c7
JM
1183 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
1184
b3f3865e 1185#ifdef ANDROID
d2a9e2c7
JM
1186 if (os_strncmp(ctrl, "@android:", 9) == 0) {
1187 priv->sock = android_get_control_socket(ctrl + 9);
1188 if (priv->sock < 0) {
1189 wpa_printf(MSG_ERROR, "Failed to open Android control "
1190 "socket '%s'", ctrl + 9);
1191 goto fail;
1192 }
1193 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
1194 ctrl + 9);
3a7414b6 1195 priv->android_control_socket = 1;
b3f3865e 1196 goto havesock;
d2a9e2c7 1197 }
b3f3865e 1198
d2a9e2c7
JM
1199 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
1200 /*
1201 * Backwards compatibility - try to open an Android control
1202 * socket and if that fails, assume this was a UNIX domain
1203 * socket instead.
1204 */
1205 priv->sock = android_get_control_socket(ctrl);
1206 if (priv->sock >= 0) {
1207 wpa_printf(MSG_DEBUG,
1208 "Using Android control socket '%s'",
1209 ctrl);
3a7414b6 1210 priv->android_control_socket = 1;
d2a9e2c7
JM
1211 goto havesock;
1212 }
1213 }
1214#endif /* ANDROID */
6fc6879b
JM
1215
1216 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1217 if (priv->sock < 0) {
2c6f8cf6 1218 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
6fc6879b
JM
1219 goto fail;
1220 }
1221
1222 os_memset(&addr, 0, sizeof(addr));
09bd6e8c 1223#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
19b9436c
SL
1224 addr.sun_len = sizeof(addr);
1225#endif /* __FreeBSD__ */
6fc6879b 1226 addr.sun_family = AF_UNIX;
d2a9e2c7
JM
1227
1228 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
1229 addr.sun_path[0] = '\0';
1230 os_strlcpy(addr.sun_path + 1, ctrl + 10,
1231 sizeof(addr.sun_path) - 1);
1232 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
1233 0) {
1234 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
2c6f8cf6
JM
1235 "bind(PF_UNIX;%s) failed: %s",
1236 ctrl, strerror(errno));
d2a9e2c7
JM
1237 goto fail;
1238 }
1239 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
1240 ctrl + 10);
1241 goto havesock;
1242 }
1243
1244 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
6fc6879b 1245 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2c6f8cf6
JM
1246 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
1247 ctrl, strerror(errno));
6fc6879b
JM
1248 if (connect(priv->sock, (struct sockaddr *) &addr,
1249 sizeof(addr)) < 0) {
1250 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1251 " allow connections - assuming it was left"
1252 "over from forced program termination");
d2a9e2c7 1253 if (unlink(ctrl) < 0) {
2c6f8cf6
JM
1254 wpa_printf(MSG_ERROR,
1255 "Could not unlink existing ctrl_iface socket '%s': %s",
1256 ctrl, strerror(errno));
6fc6879b
JM
1257 goto fail;
1258 }
1259 if (bind(priv->sock, (struct sockaddr *) &addr,
1260 sizeof(addr)) < 0) {
2c6f8cf6
JM
1261 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
1262 ctrl, strerror(errno));
6fc6879b
JM
1263 goto fail;
1264 }
1265 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1266 "ctrl_iface socket '%s'",
d2a9e2c7 1267 ctrl);
6fc6879b
JM
1268 } else {
1269 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1270 "be in use - cannot override it");
1271 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1272 "not used anymore",
d2a9e2c7 1273 ctrl);
6fc6879b
JM
1274 goto fail;
1275 }
1276 }
1277
d2a9e2c7
JM
1278 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
1279
29257565
JM
1280 if (global->params.ctrl_interface_group) {
1281 char *gid_str = global->params.ctrl_interface_group;
1282 gid_t gid = 0;
1283 struct group *grp;
1284 char *endp;
1285
1286 grp = getgrnam(gid_str);
1287 if (grp) {
1288 gid = grp->gr_gid;
1289 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1290 " (from group name '%s')",
1291 (int) gid, gid_str);
1292 } else {
1293 /* Group name not found - try to parse this as gid */
1294 gid = strtol(gid_str, &endp, 10);
1295 if (*gid_str == '\0' || *endp != '\0') {
1296 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
1297 "'%s'", gid_str);
1298 goto fail;
1299 }
1300 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1301 (int) gid);
1302 }
d2a9e2c7 1303 if (chown(ctrl, -1, gid) < 0) {
2c6f8cf6
JM
1304 wpa_printf(MSG_ERROR,
1305 "chown[global_ctrl_interface=%s,gid=%d]: %s",
1306 ctrl, (int) gid, strerror(errno));
29257565
JM
1307 goto fail;
1308 }
1309
d2a9e2c7 1310 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
2c6f8cf6
JM
1311 wpa_printf(MSG_ERROR,
1312 "chmod[global_ctrl_interface=%s]: %s",
1313 ctrl, strerror(errno));
29257565
JM
1314 goto fail;
1315 }
d2a9e2c7 1316 } else {
f62415df
JM
1317 if (chmod(ctrl, S_IRWXU) < 0) {
1318 wpa_printf(MSG_DEBUG,
1319 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1320 ctrl, strerror(errno));
1321 /* continue anyway since group change was not required
1322 */
1323 }
29257565
JM
1324 }
1325
b3f3865e 1326havesock:
8d6e0350
JM
1327
1328 /*
1329 * Make socket non-blocking so that we don't hang forever if
1330 * target dies unexpectedly.
1331 */
1332 flags = fcntl(priv->sock, F_GETFL);
1333 if (flags >= 0) {
1334 flags |= O_NONBLOCK;
1335 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
2c6f8cf6
JM
1336 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1337 strerror(errno));
8d6e0350
JM
1338 /* Not fatal, continue on.*/
1339 }
1340 }
1341
6fc6879b
JM
1342 eloop_register_read_sock(priv->sock,
1343 wpa_supplicant_global_ctrl_iface_receive,
214e428b 1344 global, priv);
6fc6879b 1345
89286e91 1346 return 0;
6fc6879b
JM
1347
1348fail:
89286e91 1349 if (priv->sock >= 0) {
6fc6879b 1350 close(priv->sock);
89286e91
JM
1351 priv->sock = -1;
1352 }
1353 return -1;
1354}
1355
1356
1357struct ctrl_iface_global_priv *
1358wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1359{
1360 struct ctrl_iface_global_priv *priv;
1361
1362 priv = os_zalloc(sizeof(*priv));
1363 if (priv == NULL)
1364 return NULL;
1365 dl_list_init(&priv->ctrl_dst);
3fdaaa8f 1366 dl_list_init(&priv->msg_queue);
89286e91
JM
1367 priv->global = global;
1368 priv->sock = -1;
1369
1370 if (global->params.ctrl_interface == NULL)
1371 return priv;
1372
1373 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1374 os_free(priv);
1375 return NULL;
1376 }
1377
76fe79ef
JM
1378 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1379
89286e91
JM
1380 return priv;
1381}
1382
1383
1384static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1385 struct ctrl_iface_global_priv *priv)
1386{
1387 int res;
1388
1389 if (priv->sock <= 0)
1390 return -1;
1391
3a7414b6
NM
1392 /*
1393 * On Android, the control socket being used may be the socket
1394 * that is created when wpa_supplicant is started as a /init.*.rc
1395 * service. Such a socket is maintained as a key-value pair in
1396 * Android's environment. Closing this control socket would leave us
1397 * in a bad state with an invalid socket descriptor.
1398 */
1399 if (priv->android_control_socket)
1400 return priv->sock;
1401
89286e91
JM
1402 eloop_unregister_read_sock(priv->sock);
1403 close(priv->sock);
1404 priv->sock = -1;
1405 res = wpas_global_ctrl_iface_open_sock(global, priv);
1406 if (res < 0)
1407 return -1;
1408 return priv->sock;
6fc6879b
JM
1409}
1410
1411
1412void
1413wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1414{
214e428b 1415 struct wpa_ctrl_dst *dst, *prev;
3fdaaa8f 1416 struct ctrl_iface_msg *msg, *prev_msg;
214e428b 1417
6fc6879b
JM
1418 if (priv->sock >= 0) {
1419 eloop_unregister_read_sock(priv->sock);
1420 close(priv->sock);
1421 }
1422 if (priv->global->params.ctrl_interface)
1423 unlink(priv->global->params.ctrl_interface);
214e428b
JM
1424 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
1425 list)
1426 os_free(dst);
3fdaaa8f
JM
1427 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
1428 struct ctrl_iface_msg, list) {
1429 dl_list_del(&msg->list);
1430 os_free(msg);
1431 }
6fc6879b
JM
1432 os_free(priv);
1433}