OSDN Git Service

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