wpa_supplicant: Use common functions for ctrl_iface
[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_un *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_un *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_un *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_un 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 addr_txt[200];
932
933                 if (level < dst->debug_level)
934                         continue;
935
936                 printf_encode(addr_txt, sizeof(addr_txt),
937                               (u8 *) dst->addr.sun_path, dst->addrlen -
938                               offsetof(struct sockaddr_un, sun_path));
939                 msg.msg_name = (void *) &dst->addr;
940                 msg.msg_namelen = dst->addrlen;
941                 wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
942                 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
943                         wpa_printf(MSG_MSGDUMP,
944                                    "CTRL_IFACE monitor sent successfully to %s",
945                                    addr_txt);
946                         dst->errors = 0;
947                         continue;
948                 }
949
950                 _errno = errno;
951                 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
952                            addr_txt, errno, strerror(errno));
953                 dst->errors++;
954
955                 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
956                         wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
957                                 addr_txt);
958                         wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
959                                                          dst->addrlen);
960                 }
961
962                 if (_errno == ENOBUFS || _errno == EAGAIN) {
963                         /*
964                          * The socket send buffer could be full. This may happen
965                          * if client programs are not receiving their pending
966                          * messages. Close and reopen the socket as a workaround
967                          * to avoid getting stuck being unable to send any new
968                          * responses.
969                          */
970                         if (priv)
971                                 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
972                         else if (gp)
973                                 sock = wpas_ctrl_iface_global_reinit(
974                                         wpa_s->global, gp);
975                         else
976                                 break;
977                         if (sock < 0) {
978                                 wpa_dbg(wpa_s, MSG_DEBUG,
979                                         "Failed to reinitialize ctrl_iface socket");
980                                 break;
981                         }
982                 }
983         }
984 }
985
986
987 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
988 {
989         char buf[256];
990         int res;
991         struct sockaddr_un from;
992         socklen_t fromlen = sizeof(from);
993
994         if (priv->sock == -1)
995                 return;
996
997         for (;;) {
998                 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
999                            "attach", priv->wpa_s->ifname);
1000                 eloop_wait_for_read_sock(priv->sock);
1001
1002                 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
1003                                (struct sockaddr *) &from, &fromlen);
1004                 if (res < 0) {
1005                         wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1006                                    strerror(errno));
1007                         continue;
1008                 }
1009                 buf[res] = '\0';
1010
1011                 if (os_strcmp(buf, "ATTACH") == 0) {
1012                         /* handle ATTACH signal of first monitor interface */
1013                         if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
1014                                                               &from, fromlen,
1015                                                               0)) {
1016                                 if (sendto(priv->sock, "OK\n", 3, 0,
1017                                            (struct sockaddr *) &from, fromlen) <
1018                                     0) {
1019                                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1020                                                    strerror(errno));
1021                                 }
1022                                 /* OK to continue */
1023                                 return;
1024                         } else {
1025                                 if (sendto(priv->sock, "FAIL\n", 5, 0,
1026                                            (struct sockaddr *) &from, fromlen) <
1027                                     0) {
1028                                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1029                                                    strerror(errno));
1030                                 }
1031                         }
1032                 } else {
1033                         /* return FAIL for all other signals */
1034                         if (sendto(priv->sock, "FAIL\n", 5, 0,
1035                                    (struct sockaddr *) &from, fromlen) < 0) {
1036                                 wpa_printf(MSG_DEBUG,
1037                                            "ctrl_iface sendto failed: %s",
1038                                            strerror(errno));
1039                         }
1040                 }
1041         }
1042 }
1043
1044
1045 /* Global ctrl_iface */
1046
1047 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1048                                                      void *sock_ctx)
1049 {
1050         struct wpa_global *global = eloop_ctx;
1051         struct ctrl_iface_global_priv *priv = sock_ctx;
1052         char buf[4096];
1053         int res;
1054         struct sockaddr_un from;
1055         socklen_t fromlen = sizeof(from);
1056         char *reply = NULL, *reply_buf = NULL;
1057         size_t reply_len;
1058
1059         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1060                        (struct sockaddr *) &from, &fromlen);
1061         if (res < 0) {
1062                 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1063                            strerror(errno));
1064                 return;
1065         }
1066         buf[res] = '\0';
1067
1068         if (os_strcmp(buf, "ATTACH") == 0) {
1069                 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
1070                                                      fromlen, 1))
1071                         reply_len = 1;
1072                 else
1073                         reply_len = 2;
1074         } else if (os_strcmp(buf, "DETACH") == 0) {
1075                 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
1076                                                      fromlen))
1077                         reply_len = 1;
1078                 else
1079                         reply_len = 2;
1080         } else {
1081                 reply_buf = wpa_supplicant_global_ctrl_iface_process(
1082                         global, buf, &reply_len);
1083                 reply = reply_buf;
1084
1085                 /*
1086                  * There could be some password/key material in the command, so
1087                  * clear the buffer explicitly now that it is not needed
1088                  * anymore.
1089                  */
1090                 os_memset(buf, 0, res);
1091         }
1092
1093         if (!reply && reply_len == 1) {
1094                 reply = "FAIL\n";
1095                 reply_len = 5;
1096         } else if (!reply && reply_len == 2) {
1097                 reply = "OK\n";
1098                 reply_len = 3;
1099         }
1100
1101         if (reply) {
1102                 wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
1103                                      sock, reply, reply_len);
1104                 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
1105                            fromlen) < 0) {
1106                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1107                                 strerror(errno));
1108                 }
1109         }
1110         os_free(reply_buf);
1111 }
1112
1113
1114 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
1115                                             struct ctrl_iface_global_priv *priv)
1116 {
1117         struct sockaddr_un addr;
1118         const char *ctrl = global->params.ctrl_interface;
1119         int flags;
1120
1121         wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
1122
1123 #ifdef ANDROID
1124         if (os_strncmp(ctrl, "@android:", 9) == 0) {
1125                 priv->sock = android_get_control_socket(ctrl + 9);
1126                 if (priv->sock < 0) {
1127                         wpa_printf(MSG_ERROR, "Failed to open Android control "
1128                                    "socket '%s'", ctrl + 9);
1129                         goto fail;
1130                 }
1131                 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
1132                            ctrl + 9);
1133                 priv->android_control_socket = 1;
1134                 goto havesock;
1135         }
1136
1137         if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
1138                 /*
1139                  * Backwards compatibility - try to open an Android control
1140                  * socket and if that fails, assume this was a UNIX domain
1141                  * socket instead.
1142                  */
1143                 priv->sock = android_get_control_socket(ctrl);
1144                 if (priv->sock >= 0) {
1145                         wpa_printf(MSG_DEBUG,
1146                                    "Using Android control socket '%s'",
1147                                    ctrl);
1148                         priv->android_control_socket = 1;
1149                         goto havesock;
1150                 }
1151         }
1152 #endif /* ANDROID */
1153
1154         priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1155         if (priv->sock < 0) {
1156                 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
1157                 goto fail;
1158         }
1159
1160         os_memset(&addr, 0, sizeof(addr));
1161 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1162         addr.sun_len = sizeof(addr);
1163 #endif /* __FreeBSD__ */
1164         addr.sun_family = AF_UNIX;
1165
1166         if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
1167                 addr.sun_path[0] = '\0';
1168                 os_strlcpy(addr.sun_path + 1, ctrl + 10,
1169                            sizeof(addr.sun_path) - 1);
1170                 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
1171                     0) {
1172                         wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
1173                                    "bind(PF_UNIX;%s) failed: %s",
1174                                    ctrl, strerror(errno));
1175                         goto fail;
1176                 }
1177                 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
1178                            ctrl + 10);
1179                 goto havesock;
1180         }
1181
1182         os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
1183         if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1184                 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
1185                            ctrl, strerror(errno));
1186                 if (connect(priv->sock, (struct sockaddr *) &addr,
1187                             sizeof(addr)) < 0) {
1188                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1189                                    " allow connections - assuming it was left"
1190                                    "over from forced program termination");
1191                         if (unlink(ctrl) < 0) {
1192                                 wpa_printf(MSG_ERROR,
1193                                            "Could not unlink existing ctrl_iface socket '%s': %s",
1194                                            ctrl, strerror(errno));
1195                                 goto fail;
1196                         }
1197                         if (bind(priv->sock, (struct sockaddr *) &addr,
1198                                  sizeof(addr)) < 0) {
1199                                 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
1200                                            ctrl, strerror(errno));
1201                                 goto fail;
1202                         }
1203                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1204                                    "ctrl_iface socket '%s'",
1205                                    ctrl);
1206                 } else {
1207                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1208                                    "be in use - cannot override it");
1209                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1210                                    "not used anymore",
1211                                    ctrl);
1212                         goto fail;
1213                 }
1214         }
1215
1216         wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
1217
1218         if (global->params.ctrl_interface_group) {
1219                 char *gid_str = global->params.ctrl_interface_group;
1220                 gid_t gid = 0;
1221                 struct group *grp;
1222                 char *endp;
1223
1224                 grp = getgrnam(gid_str);
1225                 if (grp) {
1226                         gid = grp->gr_gid;
1227                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1228                                    " (from group name '%s')",
1229                                    (int) gid, gid_str);
1230                 } else {
1231                         /* Group name not found - try to parse this as gid */
1232                         gid = strtol(gid_str, &endp, 10);
1233                         if (*gid_str == '\0' || *endp != '\0') {
1234                                 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
1235                                            "'%s'", gid_str);
1236                                 goto fail;
1237                         }
1238                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1239                                    (int) gid);
1240                 }
1241                 if (chown(ctrl, -1, gid) < 0) {
1242                         wpa_printf(MSG_ERROR,
1243                                    "chown[global_ctrl_interface=%s,gid=%d]: %s",
1244                                    ctrl, (int) gid, strerror(errno));
1245                         goto fail;
1246                 }
1247
1248                 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
1249                         wpa_printf(MSG_ERROR,
1250                                    "chmod[global_ctrl_interface=%s]: %s",
1251                                    ctrl, strerror(errno));
1252                         goto fail;
1253                 }
1254         } else {
1255                 if (chmod(ctrl, S_IRWXU) < 0) {
1256                         wpa_printf(MSG_DEBUG,
1257                                    "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1258                                    ctrl, strerror(errno));
1259                         /* continue anyway since group change was not required
1260                          */
1261                 }
1262         }
1263
1264 havesock:
1265
1266         /*
1267          * Make socket non-blocking so that we don't hang forever if
1268          * target dies unexpectedly.
1269          */
1270         flags = fcntl(priv->sock, F_GETFL);
1271         if (flags >= 0) {
1272                 flags |= O_NONBLOCK;
1273                 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1274                         wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1275                                    strerror(errno));
1276                         /* Not fatal, continue on.*/
1277                 }
1278         }
1279
1280         eloop_register_read_sock(priv->sock,
1281                                  wpa_supplicant_global_ctrl_iface_receive,
1282                                  global, priv);
1283
1284         return 0;
1285
1286 fail:
1287         if (priv->sock >= 0) {
1288                 close(priv->sock);
1289                 priv->sock = -1;
1290         }
1291         return -1;
1292 }
1293
1294
1295 struct ctrl_iface_global_priv *
1296 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1297 {
1298         struct ctrl_iface_global_priv *priv;
1299
1300         priv = os_zalloc(sizeof(*priv));
1301         if (priv == NULL)
1302                 return NULL;
1303         dl_list_init(&priv->ctrl_dst);
1304         dl_list_init(&priv->msg_queue);
1305         priv->global = global;
1306         priv->sock = -1;
1307
1308         if (global->params.ctrl_interface == NULL)
1309                 return priv;
1310
1311         if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1312                 os_free(priv);
1313                 return NULL;
1314         }
1315
1316         wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1317
1318         return priv;
1319 }
1320
1321
1322 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1323                                          struct ctrl_iface_global_priv *priv)
1324 {
1325         int res;
1326
1327         if (priv->sock <= 0)
1328                 return -1;
1329
1330         /*
1331          * On Android, the control socket being used may be the socket
1332          * that is created when wpa_supplicant is started as a /init.*.rc
1333          * service. Such a socket is maintained as a key-value pair in
1334          * Android's environment. Closing this control socket would leave us
1335          * in a bad state with an invalid socket descriptor.
1336          */
1337         if (priv->android_control_socket)
1338                 return priv->sock;
1339
1340         eloop_unregister_read_sock(priv->sock);
1341         close(priv->sock);
1342         priv->sock = -1;
1343         res = wpas_global_ctrl_iface_open_sock(global, priv);
1344         if (res < 0)
1345                 return -1;
1346         return priv->sock;
1347 }
1348
1349
1350 void
1351 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1352 {
1353         struct wpa_ctrl_dst *dst, *prev;
1354         struct ctrl_iface_msg *msg, *prev_msg;
1355
1356         if (priv->sock >= 0) {
1357                 eloop_unregister_read_sock(priv->sock);
1358                 close(priv->sock);
1359         }
1360         if (priv->global->params.ctrl_interface)
1361                 unlink(priv->global->params.ctrl_interface);
1362         dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
1363                               list) {
1364                 dl_list_del(&dst->list);
1365                 os_free(dst);
1366         }
1367         dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
1368                               struct ctrl_iface_msg, list) {
1369                 dl_list_del(&msg->list);
1370                 os_free(msg);
1371         }
1372         os_free(priv);
1373 }