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