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