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