]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface_unix.c
Interworking: Fetch only the needed ANQP information
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface_unix.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / UNIX domain socket -based control interface
09e47a07 3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10#include <sys/un.h>
11#include <sys/stat.h>
12#include <grp.h>
19b9436c 13#include <stddef.h>
4fdc8def
BG
14#include <unistd.h>
15#include <fcntl.h>
b3f3865e
DS
16#ifdef ANDROID
17#include <cutils/sockets.h>
18#endif /* ANDROID */
6fc6879b 19
09e47a07
JM
20#include "utils/common.h"
21#include "utils/eloop.h"
22#include "utils/list.h"
6fc6879b 23#include "eapol_supp/eapol_supp_sm.h"
09e47a07 24#include "config.h"
6fc6879b
JM
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 */
37struct wpa_ctrl_dst {
09e47a07 38 struct dl_list list;
6fc6879b
JM
39 struct sockaddr_un addr;
40 socklen_t addrlen;
41 int debug_level;
42 int errors;
43};
44
45
46struct ctrl_iface_priv {
47 struct wpa_supplicant *wpa_s;
48 int sock;
09e47a07 49 struct dl_list ctrl_dst;
6fc6879b
JM
50};
51
52
53static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
54 int level, const char *buf,
55 size_t len);
56
57
58static 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;
09e47a07 70 dl_list_add(&priv->ctrl_dst, &dst->list);
6fc6879b 71 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
19b9436c
SL
72 (u8 *) from->sun_path,
73 fromlen - offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
74 return 0;
75}
76
77
78static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
79 struct sockaddr_un *from,
80 socklen_t fromlen)
81{
09e47a07 82 struct wpa_ctrl_dst *dst;
6fc6879b 83
09e47a07 84 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
6fc6879b
JM
85 if (fromlen == dst->addrlen &&
86 os_memcmp(from->sun_path, dst->addr.sun_path,
19b9436c
SL
87 fromlen - offsetof(struct sockaddr_un, sun_path))
88 == 0) {
09e47a07 89 dl_list_del(&dst->list);
6fc6879b
JM
90 os_free(dst);
91 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
92 (u8 *) from->sun_path,
19b9436c
SL
93 fromlen -
94 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
95 return 0;
96 }
6fc6879b
JM
97 }
98 return -1;
99}
100
101
102static 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
09e47a07 111 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
6fc6879b
JM
112 if (fromlen == dst->addrlen &&
113 os_memcmp(from->sun_path, dst->addr.sun_path,
19b9436c
SL
114 fromlen - offsetof(struct sockaddr_un, sun_path))
115 == 0) {
6fc6879b
JM
116 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
117 "level", (u8 *) from->sun_path,
19b9436c
SL
118 fromlen -
119 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
120 dst->debug_level = atoi(level);
121 return 0;
122 }
6fc6879b
JM
123 }
124
125 return -1;
126}
127
128
129static 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;
b563b388 134 char buf[4096];
6fc6879b
JM
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
190static 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
243static 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
253struct ctrl_iface_priv *
254wpa_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;
4fdc8def 264 int flags;
6fc6879b
JM
265
266 priv = os_zalloc(sizeof(*priv));
267 if (priv == NULL)
268 return NULL;
09e47a07 269 dl_list_init(&priv->ctrl_dst);
6fc6879b
JM
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;
b3f3865e
DS
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 */
6fc6879b
JM
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 if (gid_str) {
309 grp = getgrnam(gid_str);
310 if (grp) {
311 gid = grp->gr_gid;
312 gid_set = 1;
313 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
314 " (from group name '%s')",
315 (int) gid, gid_str);
316 } else {
317 /* Group name not found - try to parse this as gid */
318 gid = strtol(gid_str, &endp, 10);
319 if (*gid_str == '\0' || *endp != '\0') {
8e888179 320 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
6fc6879b
JM
321 "'%s'", gid_str);
322 goto fail;
323 }
324 gid_set = 1;
325 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
326 (int) gid);
327 }
328 }
329
330 if (gid_set && chown(dir, -1, gid) < 0) {
331 perror("chown[ctrl_interface]");
332 goto fail;
333 }
334
3fd2a226
AAS
335 /* Make sure the group can enter and read the directory */
336 if (gid_set &&
337 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
338 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
339 strerror(errno));
340 goto fail;
341 }
342
6fc6879b
JM
343 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
344 sizeof(addr.sun_path)) {
345 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
346 goto fail;
347 }
348
349 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
350 if (priv->sock < 0) {
351 perror("socket(PF_UNIX)");
352 goto fail;
353 }
354
355 os_memset(&addr, 0, sizeof(addr));
09bd6e8c 356#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
19b9436c
SL
357 addr.sun_len = sizeof(addr);
358#endif /* __FreeBSD__ */
6fc6879b
JM
359 addr.sun_family = AF_UNIX;
360 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
361 if (fname == NULL)
362 goto fail;
363 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
364 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
365 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
366 strerror(errno));
367 if (connect(priv->sock, (struct sockaddr *) &addr,
368 sizeof(addr)) < 0) {
369 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
370 " allow connections - assuming it was left"
371 "over from forced program termination");
372 if (unlink(fname) < 0) {
373 perror("unlink[ctrl_iface]");
374 wpa_printf(MSG_ERROR, "Could not unlink "
375 "existing ctrl_iface socket '%s'",
376 fname);
377 goto fail;
378 }
379 if (bind(priv->sock, (struct sockaddr *) &addr,
380 sizeof(addr)) < 0) {
9d053747 381 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
6fc6879b
JM
382 goto fail;
383 }
384 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
385 "ctrl_iface socket '%s'", fname);
386 } else {
387 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
388 "be in use - cannot override it");
389 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
390 "not used anymore", fname);
391 os_free(fname);
392 fname = NULL;
393 goto fail;
394 }
395 }
396
397 if (gid_set && chown(fname, -1, gid) < 0) {
398 perror("chown[ctrl_interface/ifname]");
399 goto fail;
400 }
401
402 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
403 perror("chmod[ctrl_interface/ifname]");
404 goto fail;
405 }
406 os_free(fname);
407
b3f3865e
DS
408#ifdef ANDROID
409havesock:
410#endif /* ANDROID */
4fdc8def
BG
411
412 /*
413 * Make socket non-blocking so that we don't hang forever if
414 * target dies unexpectedly.
415 */
416 flags = fcntl(priv->sock, F_GETFL);
417 if (flags >= 0) {
418 flags |= O_NONBLOCK;
419 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
420 perror("fcntl(ctrl, O_NONBLOCK)");
421 /* Not fatal, continue on.*/
422 }
423 }
424
6fc6879b
JM
425 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
426 wpa_s, priv);
427 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
428
429 os_free(buf);
430 return priv;
431
432fail:
433 if (priv->sock >= 0)
434 close(priv->sock);
435 os_free(priv);
436 if (fname) {
437 unlink(fname);
438 os_free(fname);
439 }
440 os_free(buf);
441 return NULL;
442}
443
444
445void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
446{
447 struct wpa_ctrl_dst *dst, *prev;
448
449 if (priv->sock > -1) {
450 char *fname;
451 char *buf, *dir = NULL, *gid_str = NULL;
452 eloop_unregister_read_sock(priv->sock);
09e47a07 453 if (!dl_list_empty(&priv->ctrl_dst)) {
6fc6879b
JM
454 /*
455 * Wait a second before closing the control socket if
456 * there are any attached monitors in order to allow
457 * them to receive any pending messages.
458 */
459 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
460 "monitors to receive messages");
461 os_sleep(1, 0);
462 }
463 close(priv->sock);
464 priv->sock = -1;
465 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
466 if (fname) {
467 unlink(fname);
468 os_free(fname);
469 }
470
471 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
472 if (buf == NULL)
473 goto free_dst;
474 if (os_strncmp(buf, "DIR=", 4) == 0) {
475 dir = buf + 4;
476 gid_str = os_strstr(dir, " GROUP=");
477 if (gid_str) {
478 *gid_str = '\0';
479 gid_str += 7;
480 }
481 } else
482 dir = buf;
483
484 if (rmdir(dir) < 0) {
485 if (errno == ENOTEMPTY) {
486 wpa_printf(MSG_DEBUG, "Control interface "
487 "directory not empty - leaving it "
488 "behind");
489 } else {
490 perror("rmdir[ctrl_interface]");
491 }
492 }
493 os_free(buf);
494 }
495
496free_dst:
09e47a07
JM
497 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
498 list)
499 os_free(dst);
6fc6879b
JM
500 os_free(priv);
501}
502
503
504/**
505 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
506 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
507 * @level: Priority level of the message
508 * @buf: Message data
509 * @len: Message length
510 *
511 * Send a packet to all monitor programs attached to the control interface.
512 */
513static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
514 int level, const char *buf,
515 size_t len)
516{
517 struct wpa_ctrl_dst *dst, *next;
518 char levelstr[10];
519 int idx, res;
520 struct msghdr msg;
521 struct iovec io[2];
522
09e47a07 523 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
6fc6879b
JM
524 return;
525
526 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
527 if (res < 0 || (size_t) res >= sizeof(levelstr))
528 return;
529 io[0].iov_base = levelstr;
530 io[0].iov_len = os_strlen(levelstr);
531 io[1].iov_base = (char *) buf;
532 io[1].iov_len = len;
533 os_memset(&msg, 0, sizeof(msg));
534 msg.msg_iov = io;
535 msg.msg_iovlen = 2;
536
537 idx = 0;
09e47a07
JM
538 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
539 list) {
6fc6879b
JM
540 if (level >= dst->debug_level) {
541 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
542 (u8 *) dst->addr.sun_path, dst->addrlen -
19b9436c 543 offsetof(struct sockaddr_un, sun_path));
6fc6879b
JM
544 msg.msg_name = (void *) &dst->addr;
545 msg.msg_namelen = dst->addrlen;
546 if (sendmsg(priv->sock, &msg, 0) < 0) {
c5aaa015
JM
547 int _errno = errno;
548 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
549 "%d - %s",
550 idx, errno, strerror(errno));
6fc6879b 551 dst->errors++;
6700a277
JM
552 if (dst->errors > 1000 ||
553 (_errno != ENOBUFS && dst->errors > 10) ||
554 _errno == ENOENT) {
6fc6879b
JM
555 wpa_supplicant_ctrl_iface_detach(
556 priv, &dst->addr,
557 dst->addrlen);
558 }
559 } else
560 dst->errors = 0;
561 }
562 idx++;
6fc6879b
JM
563 }
564}
565
566
567void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
568{
569 char buf[256];
570 int res;
571 struct sockaddr_un from;
572 socklen_t fromlen = sizeof(from);
573
574 for (;;) {
575 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
576 "attach", priv->wpa_s->ifname);
577 eloop_wait_for_read_sock(priv->sock);
578
579 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
580 (struct sockaddr *) &from, &fromlen);
581 if (res < 0) {
582 perror("recvfrom(ctrl_iface)");
583 continue;
584 }
585 buf[res] = '\0';
586
587 if (os_strcmp(buf, "ATTACH") == 0) {
588 /* handle ATTACH signal of first monitor interface */
589 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
590 fromlen)) {
591 sendto(priv->sock, "OK\n", 3, 0,
592 (struct sockaddr *) &from, fromlen);
593 /* OK to continue */
594 return;
595 } else {
596 sendto(priv->sock, "FAIL\n", 5, 0,
597 (struct sockaddr *) &from, fromlen);
598 }
599 } else {
600 /* return FAIL for all other signals */
601 sendto(priv->sock, "FAIL\n", 5, 0,
602 (struct sockaddr *) &from, fromlen);
603 }
604 }
605}
606
607
608/* Global ctrl_iface */
609
610struct ctrl_iface_global_priv {
611 struct wpa_global *global;
612 int sock;
613};
614
615
616static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
617 void *sock_ctx)
618{
619 struct wpa_global *global = eloop_ctx;
620 char buf[256];
621 int res;
622 struct sockaddr_un from;
623 socklen_t fromlen = sizeof(from);
624 char *reply;
625 size_t reply_len;
626
627 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
628 (struct sockaddr *) &from, &fromlen);
629 if (res < 0) {
630 perror("recvfrom(ctrl_iface)");
631 return;
632 }
633 buf[res] = '\0';
634
635 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
636 &reply_len);
637
638 if (reply) {
639 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
640 fromlen);
641 os_free(reply);
642 } else if (reply_len) {
643 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
644 fromlen);
645 }
646}
647
648
649struct ctrl_iface_global_priv *
650wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
651{
652 struct ctrl_iface_global_priv *priv;
653 struct sockaddr_un addr;
654
655 priv = os_zalloc(sizeof(*priv));
656 if (priv == NULL)
657 return NULL;
658 priv->global = global;
659 priv->sock = -1;
660
661 if (global->params.ctrl_interface == NULL)
662 return priv;
663
b3f3865e
DS
664#ifdef ANDROID
665 priv->sock = android_get_control_socket(global->params.ctrl_interface);
666 if (priv->sock >= 0)
667 goto havesock;
668#endif /* ANDROID */
669
6fc6879b
JM
670 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
671 global->params.ctrl_interface);
672
673 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
674 if (priv->sock < 0) {
675 perror("socket(PF_UNIX)");
676 goto fail;
677 }
678
679 os_memset(&addr, 0, sizeof(addr));
09bd6e8c 680#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
19b9436c
SL
681 addr.sun_len = sizeof(addr);
682#endif /* __FreeBSD__ */
6fc6879b
JM
683 addr.sun_family = AF_UNIX;
684 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
685 sizeof(addr.sun_path));
686 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
9d053747
BG
687 perror("supp-global-ctrl-iface-init (will try fixup): "
688 "bind(PF_UNIX)");
6fc6879b
JM
689 if (connect(priv->sock, (struct sockaddr *) &addr,
690 sizeof(addr)) < 0) {
691 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
692 " allow connections - assuming it was left"
693 "over from forced program termination");
694 if (unlink(global->params.ctrl_interface) < 0) {
695 perror("unlink[ctrl_iface]");
696 wpa_printf(MSG_ERROR, "Could not unlink "
697 "existing ctrl_iface socket '%s'",
698 global->params.ctrl_interface);
699 goto fail;
700 }
701 if (bind(priv->sock, (struct sockaddr *) &addr,
702 sizeof(addr)) < 0) {
9d053747 703 perror("supp-glb-iface-init: bind(PF_UNIX)");
6fc6879b
JM
704 goto fail;
705 }
706 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
707 "ctrl_iface socket '%s'",
708 global->params.ctrl_interface);
709 } else {
710 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
711 "be in use - cannot override it");
712 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
713 "not used anymore",
714 global->params.ctrl_interface);
715 goto fail;
716 }
717 }
718
b3f3865e
DS
719#ifdef ANDROID
720havesock:
721#endif /* ANDROID */
6fc6879b
JM
722 eloop_register_read_sock(priv->sock,
723 wpa_supplicant_global_ctrl_iface_receive,
724 global, NULL);
725
726 return priv;
727
728fail:
729 if (priv->sock >= 0)
730 close(priv->sock);
731 os_free(priv);
732 return NULL;
733}
734
735
736void
737wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
738{
739 if (priv->sock >= 0) {
740 eloop_unregister_read_sock(priv->sock);
741 close(priv->sock);
742 }
743 if (priv->global->params.ctrl_interface)
744 unlink(priv->global->params.ctrl_interface);
745 os_free(priv);
746}