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