OSDN Git Service

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