Stop ctrl_iface monitor send loop on reinit failure
[mech_eap.git] / wpa_supplicant / ctrl_iface_unix.c
1 /*
2  * WPA Supplicant / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2013, 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 ANDROID
17 #include <cutils/sockets.h>
18 #endif /* ANDROID */
19
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "utils/list.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "config.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
27
28 /* Per-interface ctrl_iface */
29
30 /**
31  * struct wpa_ctrl_dst - Internal data structure of control interface monitors
32  *
33  * This structure is used to store information about registered control
34  * interface monitors into struct wpa_supplicant. This data is private to
35  * ctrl_iface_unix.c and should not be touched directly from other files.
36  */
37 struct wpa_ctrl_dst {
38         struct dl_list list;
39         struct sockaddr_un addr;
40         socklen_t addrlen;
41         int debug_level;
42         int errors;
43 };
44
45
46 struct ctrl_iface_priv {
47         struct wpa_supplicant *wpa_s;
48         int sock;
49         struct dl_list ctrl_dst;
50 };
51
52
53 struct ctrl_iface_global_priv {
54         struct wpa_global *global;
55         int sock;
56         struct dl_list ctrl_dst;
57 };
58
59
60 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
61                                            const char *ifname, int sock,
62                                            struct dl_list *ctrl_dst,
63                                            int level, const char *buf,
64                                            size_t len,
65                                            struct ctrl_iface_priv *priv,
66                                            struct ctrl_iface_global_priv *gp);
67 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
68                                   struct ctrl_iface_priv *priv);
69 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
70                                          struct ctrl_iface_global_priv *priv);
71
72
73 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
74                                             struct sockaddr_un *from,
75                                             socklen_t fromlen)
76 {
77         struct wpa_ctrl_dst *dst;
78
79         dst = os_zalloc(sizeof(*dst));
80         if (dst == NULL)
81                 return -1;
82         os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
83         dst->addrlen = fromlen;
84         dst->debug_level = MSG_INFO;
85         dl_list_add(ctrl_dst, &dst->list);
86         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
87                     (u8 *) from->sun_path,
88                     fromlen - offsetof(struct sockaddr_un, sun_path));
89         return 0;
90 }
91
92
93 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
94                                             struct sockaddr_un *from,
95                                             socklen_t fromlen)
96 {
97         struct wpa_ctrl_dst *dst;
98
99         dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
100                 if (fromlen == dst->addrlen &&
101                     os_memcmp(from->sun_path, dst->addr.sun_path,
102                               fromlen - offsetof(struct sockaddr_un, sun_path))
103                     == 0) {
104                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
105                                     (u8 *) from->sun_path,
106                                     fromlen -
107                                     offsetof(struct sockaddr_un, sun_path));
108                         dl_list_del(&dst->list);
109                         os_free(dst);
110                         return 0;
111                 }
112         }
113         return -1;
114 }
115
116
117 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
118                                            struct sockaddr_un *from,
119                                            socklen_t fromlen,
120                                            char *level)
121 {
122         struct wpa_ctrl_dst *dst;
123
124         wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
125
126         dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
127                 if (fromlen == dst->addrlen &&
128                     os_memcmp(from->sun_path, dst->addr.sun_path,
129                               fromlen - offsetof(struct sockaddr_un, sun_path))
130                     == 0) {
131                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
132                                     "level", (u8 *) from->sun_path,
133                                     fromlen -
134                                     offsetof(struct sockaddr_un, sun_path));
135                         dst->debug_level = atoi(level);
136                         return 0;
137                 }
138         }
139
140         return -1;
141 }
142
143
144 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
145                                               void *sock_ctx)
146 {
147         struct wpa_supplicant *wpa_s = eloop_ctx;
148         struct ctrl_iface_priv *priv = sock_ctx;
149         char buf[4096];
150         int res;
151         struct sockaddr_un from;
152         socklen_t fromlen = sizeof(from);
153         char *reply = NULL, *reply_buf = NULL;
154         size_t reply_len = 0;
155         int new_attached = 0;
156
157         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
158                        (struct sockaddr *) &from, &fromlen);
159         if (res < 0) {
160                 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
161                            strerror(errno));
162                 return;
163         }
164         buf[res] = '\0';
165
166         if (os_strcmp(buf, "ATTACH") == 0) {
167                 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
168                                                      fromlen))
169                         reply_len = 1;
170                 else {
171                         new_attached = 1;
172                         reply_len = 2;
173                 }
174         } else if (os_strcmp(buf, "DETACH") == 0) {
175                 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
176                                                      fromlen))
177                         reply_len = 1;
178                 else
179                         reply_len = 2;
180         } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
181                 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
182                                                     buf + 6))
183                         reply_len = 1;
184                 else
185                         reply_len = 2;
186         } else {
187                 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
188                                                               &reply_len);
189                 reply = reply_buf;
190         }
191
192         if (!reply && reply_len == 1) {
193                 reply = "FAIL\n";
194                 reply_len = 5;
195         } else if (!reply && reply_len == 2) {
196                 reply = "OK\n";
197                 reply_len = 3;
198         }
199
200         if (reply) {
201                 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
202                            fromlen) < 0) {
203                         int _errno = errno;
204                         wpa_dbg(wpa_s, MSG_DEBUG,
205                                 "ctrl_iface sendto failed: %d - %s",
206                                 _errno, strerror(_errno));
207                         if (_errno == ENOBUFS || _errno == EAGAIN) {
208                                 /*
209                                  * The socket send buffer could be full. This
210                                  * may happen if client programs are not
211                                  * receiving their pending messages. Close and
212                                  * reopen the socket as a workaround to avoid
213                                  * getting stuck being unable to send any new
214                                  * responses.
215                                  */
216                                 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
217                                 if (sock < 0) {
218                                         wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
219                                 }
220                         }
221                         if (new_attached) {
222                                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
223                                 new_attached = 0;
224                                 wpa_supplicant_ctrl_iface_detach(
225                                         &priv->ctrl_dst, &from, fromlen);
226                         }
227                 }
228         }
229         os_free(reply_buf);
230
231         if (new_attached)
232                 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
233 }
234
235
236 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
237 {
238         char *buf;
239         size_t len;
240         char *pbuf, *dir = NULL, *gid_str = NULL;
241         int res;
242
243         if (wpa_s->conf->ctrl_interface == NULL)
244                 return NULL;
245
246         pbuf = os_strdup(wpa_s->conf->ctrl_interface);
247         if (pbuf == NULL)
248                 return NULL;
249         if (os_strncmp(pbuf, "DIR=", 4) == 0) {
250                 dir = pbuf + 4;
251                 gid_str = os_strstr(dir, " GROUP=");
252                 if (gid_str) {
253                         *gid_str = '\0';
254                         gid_str += 7;
255                 }
256         } else
257                 dir = pbuf;
258
259         len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
260         buf = os_malloc(len);
261         if (buf == NULL) {
262                 os_free(pbuf);
263                 return NULL;
264         }
265
266         res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
267         if (res < 0 || (size_t) res >= len) {
268                 os_free(pbuf);
269                 os_free(buf);
270                 return NULL;
271         }
272 #ifdef __CYGWIN__
273         {
274                 /* Windows/WinPcap uses interface names that are not suitable
275                  * as a file name - convert invalid chars to underscores */
276                 char *pos = buf;
277                 while (*pos) {
278                         if (*pos == '\\')
279                                 *pos = '_';
280                         pos++;
281                 }
282         }
283 #endif /* __CYGWIN__ */
284         os_free(pbuf);
285         return buf;
286 }
287
288
289 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
290                                              const char *txt, size_t len)
291 {
292         struct wpa_supplicant *wpa_s = ctx;
293
294         if (wpa_s == NULL)
295                 return;
296
297         if (global != 2 && wpa_s->global->ctrl_iface) {
298                 struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
299                 if (!dl_list_empty(&priv->ctrl_dst)) {
300                         wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL :
301                                                        wpa_s->ifname,
302                                                        priv->sock,
303                                                        &priv->ctrl_dst,
304                                                        level, txt, len, NULL,
305                                                        priv);
306                 }
307         }
308
309         if (wpa_s->ctrl_iface == NULL)
310                 return;
311         wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
312                                        &wpa_s->ctrl_iface->ctrl_dst,
313                                        level, txt, len, wpa_s->ctrl_iface,
314                                        NULL);
315 }
316
317
318 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
319                                      struct ctrl_iface_priv *priv)
320 {
321         struct sockaddr_un addr;
322         char *fname = NULL;
323         gid_t gid = 0;
324         int gid_set = 0;
325         char *buf, *dir = NULL, *gid_str = NULL;
326         struct group *grp;
327         char *endp;
328         int flags;
329
330         buf = os_strdup(wpa_s->conf->ctrl_interface);
331         if (buf == NULL)
332                 goto fail;
333 #ifdef ANDROID
334         os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
335                     wpa_s->conf->ctrl_interface);
336         priv->sock = android_get_control_socket(addr.sun_path);
337         if (priv->sock >= 0)
338                 goto havesock;
339 #endif /* ANDROID */
340         if (os_strncmp(buf, "DIR=", 4) == 0) {
341                 dir = buf + 4;
342                 gid_str = os_strstr(dir, " GROUP=");
343                 if (gid_str) {
344                         *gid_str = '\0';
345                         gid_str += 7;
346                 }
347         } else {
348                 dir = buf;
349                 gid_str = wpa_s->conf->ctrl_interface_group;
350         }
351
352         if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
353                 if (errno == EEXIST) {
354                         wpa_printf(MSG_DEBUG, "Using existing control "
355                                    "interface directory.");
356                 } else {
357                         wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
358                                    dir, strerror(errno));
359                         goto fail;
360                 }
361         }
362
363 #ifdef ANDROID
364         /*
365          * wpa_supplicant is started from /init.*.rc on Android and that seems
366          * to be using umask 0077 which would leave the control interface
367          * directory without group access. This breaks things since Wi-Fi
368          * framework assumes that this directory can be accessed by other
369          * applications in the wifi group. Fix this by adding group access even
370          * if umask value would prevent this.
371          */
372         if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
373                 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
374                            strerror(errno));
375                 /* Try to continue anyway */
376         }
377 #endif /* ANDROID */
378
379         if (gid_str) {
380                 grp = getgrnam(gid_str);
381                 if (grp) {
382                         gid = grp->gr_gid;
383                         gid_set = 1;
384                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
385                                    " (from group name '%s')",
386                                    (int) gid, gid_str);
387                 } else {
388                         /* Group name not found - try to parse this as gid */
389                         gid = strtol(gid_str, &endp, 10);
390                         if (*gid_str == '\0' || *endp != '\0') {
391                                 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
392                                            "'%s'", gid_str);
393                                 goto fail;
394                         }
395                         gid_set = 1;
396                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
397                                    (int) gid);
398                 }
399         }
400
401         if (gid_set && chown(dir, -1, gid) < 0) {
402                 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
403                            dir, (int) gid, strerror(errno));
404                 goto fail;
405         }
406
407         /* Make sure the group can enter and read the directory */
408         if (gid_set &&
409             chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
410                 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
411                            strerror(errno));
412                 goto fail;
413         }
414
415         if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
416             sizeof(addr.sun_path)) {
417                 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
418                 goto fail;
419         }
420
421         priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
422         if (priv->sock < 0) {
423                 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
424                 goto fail;
425         }
426
427         os_memset(&addr, 0, sizeof(addr));
428 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
429         addr.sun_len = sizeof(addr);
430 #endif /* __FreeBSD__ */
431         addr.sun_family = AF_UNIX;
432         fname = wpa_supplicant_ctrl_iface_path(wpa_s);
433         if (fname == NULL)
434                 goto fail;
435         os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
436         if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
437                 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
438                            strerror(errno));
439                 if (connect(priv->sock, (struct sockaddr *) &addr,
440                             sizeof(addr)) < 0) {
441                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
442                                    " allow connections - assuming it was left"
443                                    "over from forced program termination");
444                         if (unlink(fname) < 0) {
445                                 wpa_printf(MSG_ERROR,
446                                            "Could not unlink existing ctrl_iface socket '%s': %s",
447                                            fname, strerror(errno));
448                                 goto fail;
449                         }
450                         if (bind(priv->sock, (struct sockaddr *) &addr,
451                                  sizeof(addr)) < 0) {
452                                 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
453                                            strerror(errno));
454                                 goto fail;
455                         }
456                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
457                                    "ctrl_iface socket '%s'", fname);
458                 } else {
459                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
460                                    "be in use - cannot override it");
461                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
462                                    "not used anymore", fname);
463                         os_free(fname);
464                         fname = NULL;
465                         goto fail;
466                 }
467         }
468
469         if (gid_set && chown(fname, -1, gid) < 0) {
470                 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
471                            fname, (int) gid, strerror(errno));
472                 goto fail;
473         }
474
475         if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
476                 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
477                            fname, strerror(errno));
478                 goto fail;
479         }
480         os_free(fname);
481
482 #ifdef ANDROID
483 havesock:
484 #endif /* ANDROID */
485
486         /*
487          * Make socket non-blocking so that we don't hang forever if
488          * target dies unexpectedly.
489          */
490         flags = fcntl(priv->sock, F_GETFL);
491         if (flags >= 0) {
492                 flags |= O_NONBLOCK;
493                 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
494                         wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
495                                    strerror(errno));
496                         /* Not fatal, continue on.*/
497                 }
498         }
499
500         eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
501                                  wpa_s, priv);
502         wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
503
504         os_free(buf);
505         return 0;
506
507 fail:
508         if (priv->sock >= 0) {
509                 close(priv->sock);
510                 priv->sock = -1;
511         }
512         if (fname) {
513                 unlink(fname);
514                 os_free(fname);
515         }
516         os_free(buf);
517         return -1;
518 }
519
520
521 struct ctrl_iface_priv *
522 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
523 {
524         struct ctrl_iface_priv *priv;
525
526         priv = os_zalloc(sizeof(*priv));
527         if (priv == NULL)
528                 return NULL;
529         dl_list_init(&priv->ctrl_dst);
530         priv->wpa_s = wpa_s;
531         priv->sock = -1;
532
533         if (wpa_s->conf->ctrl_interface == NULL)
534                 return priv;
535
536         if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
537                 os_free(priv);
538                 return NULL;
539         }
540
541         return priv;
542 }
543
544
545 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
546                                   struct ctrl_iface_priv *priv)
547 {
548         int res;
549
550         if (priv->sock <= 0)
551                 return -1;
552
553         eloop_unregister_read_sock(priv->sock);
554         close(priv->sock);
555         priv->sock = -1;
556         res = wpas_ctrl_iface_open_sock(wpa_s, priv);
557         if (res < 0)
558                 return -1;
559         return priv->sock;
560 }
561
562
563 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
564 {
565         struct wpa_ctrl_dst *dst, *prev;
566
567         if (priv->sock > -1) {
568                 char *fname;
569                 char *buf, *dir = NULL, *gid_str = NULL;
570                 eloop_unregister_read_sock(priv->sock);
571                 if (!dl_list_empty(&priv->ctrl_dst)) {
572                         /*
573                          * Wait before closing the control socket if
574                          * there are any attached monitors in order to allow
575                          * them to receive any pending messages.
576                          */
577                         wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
578                                    "monitors to receive messages");
579                         os_sleep(0, 100000);
580                 }
581                 close(priv->sock);
582                 priv->sock = -1;
583                 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
584                 if (fname) {
585                         unlink(fname);
586                         os_free(fname);
587                 }
588
589                 if (priv->wpa_s->conf->ctrl_interface == NULL)
590                         goto free_dst;
591                 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
592                 if (buf == NULL)
593                         goto free_dst;
594                 if (os_strncmp(buf, "DIR=", 4) == 0) {
595                         dir = buf + 4;
596                         gid_str = os_strstr(dir, " GROUP=");
597                         if (gid_str) {
598                                 *gid_str = '\0';
599                                 gid_str += 7;
600                         }
601                 } else
602                         dir = buf;
603
604                 if (rmdir(dir) < 0) {
605                         if (errno == ENOTEMPTY) {
606                                 wpa_printf(MSG_DEBUG, "Control interface "
607                                            "directory not empty - leaving it "
608                                            "behind");
609                         } else {
610                                 wpa_printf(MSG_ERROR,
611                                            "rmdir[ctrl_interface=%s]: %s",
612                                            dir, strerror(errno));
613                         }
614                 }
615                 os_free(buf);
616         }
617
618 free_dst:
619         dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
620                               list)
621                 os_free(dst);
622         os_free(priv);
623 }
624
625
626 /**
627  * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
628  * @ifname: Interface name for global control socket or %NULL
629  * @sock: Local socket fd
630  * @ctrl_dst: List of attached listeners
631  * @level: Priority level of the message
632  * @buf: Message data
633  * @len: Message length
634  *
635  * Send a packet to all monitor programs attached to the control interface.
636  */
637 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
638                                            const char *ifname, int sock,
639                                            struct dl_list *ctrl_dst,
640                                            int level, const char *buf,
641                                            size_t len,
642                                            struct ctrl_iface_priv *priv,
643                                            struct ctrl_iface_global_priv *gp)
644 {
645         struct wpa_ctrl_dst *dst, *next;
646         char levelstr[10];
647         int idx, res;
648         struct msghdr msg;
649         struct iovec io[5];
650
651         if (sock < 0 || dl_list_empty(ctrl_dst))
652                 return;
653
654         res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
655         if (res < 0 || (size_t) res >= sizeof(levelstr))
656                 return;
657         idx = 0;
658         if (ifname) {
659                 io[idx].iov_base = "IFNAME=";
660                 io[idx].iov_len = 7;
661                 idx++;
662                 io[idx].iov_base = (char *) ifname;
663                 io[idx].iov_len = os_strlen(ifname);
664                 idx++;
665                 io[idx].iov_base = " ";
666                 io[idx].iov_len = 1;
667                 idx++;
668         }
669         io[idx].iov_base = levelstr;
670         io[idx].iov_len = os_strlen(levelstr);
671         idx++;
672         io[idx].iov_base = (char *) buf;
673         io[idx].iov_len = len;
674         idx++;
675         os_memset(&msg, 0, sizeof(msg));
676         msg.msg_iov = io;
677         msg.msg_iovlen = idx;
678
679         idx = -1;
680         dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
681                 int _errno;
682
683                 idx++;
684                 if (level < dst->debug_level)
685                         continue;
686
687                 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
688                             (u8 *) dst->addr.sun_path, dst->addrlen -
689                             offsetof(struct sockaddr_un, sun_path));
690                 msg.msg_name = (void *) &dst->addr;
691                 msg.msg_namelen = dst->addrlen;
692                 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
693                         dst->errors = 0;
694                         idx++;
695                         continue;
696                 }
697
698                 _errno = errno;
699                 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: %d - %s",
700                            idx, errno, strerror(errno));
701                 dst->errors++;
702
703                 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
704                         wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor that cannot receive messages");
705                         wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
706                                                          dst->addrlen);
707                 }
708
709                 if (_errno == ENOBUFS || _errno == EAGAIN) {
710                         /*
711                          * The socket send buffer could be full. This may happen
712                          * if client programs are not receiving their pending
713                          * messages. Close and reopen the socket as a workaround
714                          * to avoid getting stuck being unable to send any new
715                          * responses.
716                          */
717                         if (priv)
718                                 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
719                         else if (gp)
720                                 sock = wpas_ctrl_iface_global_reinit(
721                                         wpa_s->global, gp);
722                         else
723                                 break;
724                         if (sock < 0) {
725                                 wpa_dbg(wpa_s, MSG_DEBUG,
726                                         "Failed to reinitialize ctrl_iface socket");
727                                 break;
728                         }
729                 }
730         }
731 }
732
733
734 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
735 {
736         char buf[256];
737         int res;
738         struct sockaddr_un from;
739         socklen_t fromlen = sizeof(from);
740
741         for (;;) {
742                 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
743                            "attach", priv->wpa_s->ifname);
744                 eloop_wait_for_read_sock(priv->sock);
745
746                 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
747                                (struct sockaddr *) &from, &fromlen);
748                 if (res < 0) {
749                         wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
750                                    strerror(errno));
751                         continue;
752                 }
753                 buf[res] = '\0';
754
755                 if (os_strcmp(buf, "ATTACH") == 0) {
756                         /* handle ATTACH signal of first monitor interface */
757                         if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
758                                                               &from, fromlen)) {
759                                 if (sendto(priv->sock, "OK\n", 3, 0,
760                                            (struct sockaddr *) &from, fromlen) <
761                                     0) {
762                                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
763                                                    strerror(errno));
764                                 }
765                                 /* OK to continue */
766                                 return;
767                         } else {
768                                 if (sendto(priv->sock, "FAIL\n", 5, 0,
769                                            (struct sockaddr *) &from, fromlen) <
770                                     0) {
771                                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
772                                                    strerror(errno));
773                                 }
774                         }
775                 } else {
776                         /* return FAIL for all other signals */
777                         if (sendto(priv->sock, "FAIL\n", 5, 0,
778                                    (struct sockaddr *) &from, fromlen) < 0) {
779                                 wpa_printf(MSG_DEBUG,
780                                            "ctrl_iface sendto failed: %s",
781                                            strerror(errno));
782                         }
783                 }
784         }
785 }
786
787
788 /* Global ctrl_iface */
789
790 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
791                                                      void *sock_ctx)
792 {
793         struct wpa_global *global = eloop_ctx;
794         struct ctrl_iface_global_priv *priv = sock_ctx;
795         char buf[256];
796         int res;
797         struct sockaddr_un from;
798         socklen_t fromlen = sizeof(from);
799         char *reply = NULL, *reply_buf = NULL;
800         size_t reply_len;
801
802         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
803                        (struct sockaddr *) &from, &fromlen);
804         if (res < 0) {
805                 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
806                            strerror(errno));
807                 return;
808         }
809         buf[res] = '\0';
810
811         if (os_strcmp(buf, "ATTACH") == 0) {
812                 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
813                                                      fromlen))
814                         reply_len = 1;
815                 else
816                         reply_len = 2;
817         } else if (os_strcmp(buf, "DETACH") == 0) {
818                 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
819                                                      fromlen))
820                         reply_len = 1;
821                 else
822                         reply_len = 2;
823         } else {
824                 reply_buf = wpa_supplicant_global_ctrl_iface_process(
825                         global, buf, &reply_len);
826                 reply = reply_buf;
827         }
828
829         if (!reply && reply_len == 1) {
830                 reply = "FAIL\n";
831                 reply_len = 5;
832         } else if (!reply && reply_len == 2) {
833                 reply = "OK\n";
834                 reply_len = 3;
835         }
836
837         if (reply) {
838                 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
839                            fromlen) < 0) {
840                         wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
841                                 strerror(errno));
842                 }
843         }
844         os_free(reply_buf);
845 }
846
847
848 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
849                                             struct ctrl_iface_global_priv *priv)
850 {
851         struct sockaddr_un addr;
852         const char *ctrl = global->params.ctrl_interface;
853         int flags;
854
855         wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
856
857 #ifdef ANDROID
858         if (os_strncmp(ctrl, "@android:", 9) == 0) {
859                 priv->sock = android_get_control_socket(ctrl + 9);
860                 if (priv->sock < 0) {
861                         wpa_printf(MSG_ERROR, "Failed to open Android control "
862                                    "socket '%s'", ctrl + 9);
863                         goto fail;
864                 }
865                 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
866                            ctrl + 9);
867                 goto havesock;
868         }
869
870         if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
871                 /*
872                  * Backwards compatibility - try to open an Android control
873                  * socket and if that fails, assume this was a UNIX domain
874                  * socket instead.
875                  */
876                 priv->sock = android_get_control_socket(ctrl);
877                 if (priv->sock >= 0) {
878                         wpa_printf(MSG_DEBUG,
879                                    "Using Android control socket '%s'",
880                                    ctrl);
881                         goto havesock;
882                 }
883         }
884 #endif /* ANDROID */
885
886         priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
887         if (priv->sock < 0) {
888                 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
889                 goto fail;
890         }
891
892         os_memset(&addr, 0, sizeof(addr));
893 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
894         addr.sun_len = sizeof(addr);
895 #endif /* __FreeBSD__ */
896         addr.sun_family = AF_UNIX;
897
898         if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
899                 addr.sun_path[0] = '\0';
900                 os_strlcpy(addr.sun_path + 1, ctrl + 10,
901                            sizeof(addr.sun_path) - 1);
902                 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
903                     0) {
904                         wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
905                                    "bind(PF_UNIX;%s) failed: %s",
906                                    ctrl, strerror(errno));
907                         goto fail;
908                 }
909                 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
910                            ctrl + 10);
911                 goto havesock;
912         }
913
914         os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
915         if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
916                 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
917                            ctrl, strerror(errno));
918                 if (connect(priv->sock, (struct sockaddr *) &addr,
919                             sizeof(addr)) < 0) {
920                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
921                                    " allow connections - assuming it was left"
922                                    "over from forced program termination");
923                         if (unlink(ctrl) < 0) {
924                                 wpa_printf(MSG_ERROR,
925                                            "Could not unlink existing ctrl_iface socket '%s': %s",
926                                            ctrl, strerror(errno));
927                                 goto fail;
928                         }
929                         if (bind(priv->sock, (struct sockaddr *) &addr,
930                                  sizeof(addr)) < 0) {
931                                 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
932                                            ctrl, strerror(errno));
933                                 goto fail;
934                         }
935                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
936                                    "ctrl_iface socket '%s'",
937                                    ctrl);
938                 } else {
939                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
940                                    "be in use - cannot override it");
941                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
942                                    "not used anymore",
943                                    ctrl);
944                         goto fail;
945                 }
946         }
947
948         wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
949
950         if (global->params.ctrl_interface_group) {
951                 char *gid_str = global->params.ctrl_interface_group;
952                 gid_t gid = 0;
953                 struct group *grp;
954                 char *endp;
955
956                 grp = getgrnam(gid_str);
957                 if (grp) {
958                         gid = grp->gr_gid;
959                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
960                                    " (from group name '%s')",
961                                    (int) gid, gid_str);
962                 } else {
963                         /* Group name not found - try to parse this as gid */
964                         gid = strtol(gid_str, &endp, 10);
965                         if (*gid_str == '\0' || *endp != '\0') {
966                                 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
967                                            "'%s'", gid_str);
968                                 goto fail;
969                         }
970                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
971                                    (int) gid);
972                 }
973                 if (chown(ctrl, -1, gid) < 0) {
974                         wpa_printf(MSG_ERROR,
975                                    "chown[global_ctrl_interface=%s,gid=%d]: %s",
976                                    ctrl, (int) gid, strerror(errno));
977                         goto fail;
978                 }
979
980                 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
981                         wpa_printf(MSG_ERROR,
982                                    "chmod[global_ctrl_interface=%s]: %s",
983                                    ctrl, strerror(errno));
984                         goto fail;
985                 }
986         } else {
987                 chmod(ctrl, S_IRWXU);
988         }
989
990 havesock:
991
992         /*
993          * Make socket non-blocking so that we don't hang forever if
994          * target dies unexpectedly.
995          */
996         flags = fcntl(priv->sock, F_GETFL);
997         if (flags >= 0) {
998                 flags |= O_NONBLOCK;
999                 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1000                         wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1001                                    strerror(errno));
1002                         /* Not fatal, continue on.*/
1003                 }
1004         }
1005
1006         eloop_register_read_sock(priv->sock,
1007                                  wpa_supplicant_global_ctrl_iface_receive,
1008                                  global, priv);
1009
1010         return 0;
1011
1012 fail:
1013         if (priv->sock >= 0) {
1014                 close(priv->sock);
1015                 priv->sock = -1;
1016         }
1017         return -1;
1018 }
1019
1020
1021 struct ctrl_iface_global_priv *
1022 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1023 {
1024         struct ctrl_iface_global_priv *priv;
1025
1026         priv = os_zalloc(sizeof(*priv));
1027         if (priv == NULL)
1028                 return NULL;
1029         dl_list_init(&priv->ctrl_dst);
1030         priv->global = global;
1031         priv->sock = -1;
1032
1033         if (global->params.ctrl_interface == NULL)
1034                 return priv;
1035
1036         if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1037                 os_free(priv);
1038                 return NULL;
1039         }
1040
1041         wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1042
1043         return priv;
1044 }
1045
1046
1047 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1048                                          struct ctrl_iface_global_priv *priv)
1049 {
1050         int res;
1051
1052         if (priv->sock <= 0)
1053                 return -1;
1054
1055         eloop_unregister_read_sock(priv->sock);
1056         close(priv->sock);
1057         priv->sock = -1;
1058         res = wpas_global_ctrl_iface_open_sock(global, priv);
1059         if (res < 0)
1060                 return -1;
1061         return priv->sock;
1062 }
1063
1064
1065 void
1066 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1067 {
1068         struct wpa_ctrl_dst *dst, *prev;
1069
1070         if (priv->sock >= 0) {
1071                 eloop_unregister_read_sock(priv->sock);
1072                 close(priv->sock);
1073         }
1074         if (priv->global->params.ctrl_interface)
1075                 unlink(priv->global->params.ctrl_interface);
1076         dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
1077                               list)
1078                 os_free(dst);
1079         os_free(priv);
1080 }