]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant / UNIX domain socket -based control interface | |
c9b55597 | 3 | * Copyright (c) 2004-2014, 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 | */ | |
37 | struct 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 | ||
46 | struct ctrl_iface_priv { | |
47 | struct wpa_supplicant *wpa_s; | |
48 | int sock; | |
09e47a07 | 49 | struct dl_list ctrl_dst; |
6fc6879b JM |
50 | }; |
51 | ||
52 | ||
214e428b JM |
53 | struct ctrl_iface_global_priv { |
54 | struct wpa_global *global; | |
55 | int sock; | |
56 | struct dl_list ctrl_dst; | |
57 | }; | |
58 | ||
59 | ||
89286e91 JM |
60 | static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s, |
61 | const char *ifname, int sock, | |
214e428b | 62 | struct dl_list *ctrl_dst, |
6fc6879b | 63 | int level, const char *buf, |
89286e91 JM |
64 | size_t len, |
65 | struct ctrl_iface_priv *priv, | |
66 | struct ctrl_iface_global_priv *gp); | |
67 | static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s, | |
68 | struct ctrl_iface_priv *priv); | |
69 | static int wpas_ctrl_iface_global_reinit(struct wpa_global *global, | |
70 | struct ctrl_iface_global_priv *priv); | |
6fc6879b JM |
71 | |
72 | ||
214e428b | 73 | static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst, |
6fc6879b JM |
74 | struct sockaddr_un *from, |
75 | socklen_t fromlen) | |
76 | { | |
77 | struct wpa_ctrl_dst *dst; | |
c9b55597 | 78 | char addr_txt[200]; |
6fc6879b JM |
79 | |
80 | dst = os_zalloc(sizeof(*dst)); | |
81 | if (dst == NULL) | |
82 | return -1; | |
83 | os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); | |
84 | dst->addrlen = fromlen; | |
85 | dst->debug_level = MSG_INFO; | |
214e428b | 86 | dl_list_add(ctrl_dst, &dst->list); |
c9b55597 JM |
87 | printf_encode(addr_txt, sizeof(addr_txt), |
88 | (u8 *) from->sun_path, | |
89 | fromlen - offsetof(struct sockaddr_un, sun_path)); | |
90 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s", addr_txt); | |
6fc6879b JM |
91 | return 0; |
92 | } | |
93 | ||
94 | ||
214e428b | 95 | static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst, |
6fc6879b JM |
96 | struct sockaddr_un *from, |
97 | socklen_t fromlen) | |
98 | { | |
09e47a07 | 99 | struct wpa_ctrl_dst *dst; |
6fc6879b | 100 | |
214e428b | 101 | dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
102 | if (fromlen == dst->addrlen && |
103 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
104 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
105 | == 0) { | |
c9b55597 JM |
106 | char addr_txt[200]; |
107 | printf_encode(addr_txt, sizeof(addr_txt), | |
108 | (u8 *) from->sun_path, | |
109 | fromlen - | |
110 | offsetof(struct sockaddr_un, sun_path)); | |
111 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s", | |
112 | addr_txt); | |
a235aca3 JM |
113 | dl_list_del(&dst->list); |
114 | os_free(dst); | |
6fc6879b JM |
115 | return 0; |
116 | } | |
6fc6879b JM |
117 | } |
118 | return -1; | |
119 | } | |
120 | ||
121 | ||
122 | static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv, | |
123 | struct sockaddr_un *from, | |
124 | socklen_t fromlen, | |
125 | char *level) | |
126 | { | |
127 | struct wpa_ctrl_dst *dst; | |
128 | ||
129 | wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); | |
130 | ||
09e47a07 | 131 | dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
132 | if (fromlen == dst->addrlen && |
133 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
134 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
135 | == 0) { | |
c9b55597 | 136 | char addr_txt[200]; |
6fc6879b | 137 | dst->debug_level = atoi(level); |
c9b55597 JM |
138 | printf_encode(addr_txt, sizeof(addr_txt), |
139 | (u8 *) from->sun_path, fromlen - | |
140 | offsetof(struct sockaddr_un, sun_path)); | |
141 | wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s", | |
142 | dst->debug_level, addr_txt); | |
6fc6879b JM |
143 | return 0; |
144 | } | |
6fc6879b JM |
145 | } |
146 | ||
147 | return -1; | |
148 | } | |
149 | ||
150 | ||
151 | static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, | |
152 | void *sock_ctx) | |
153 | { | |
154 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
155 | struct ctrl_iface_priv *priv = sock_ctx; | |
b563b388 | 156 | char buf[4096]; |
6fc6879b JM |
157 | int res; |
158 | struct sockaddr_un from; | |
159 | socklen_t fromlen = sizeof(from); | |
742e715b | 160 | char *reply = NULL, *reply_buf = NULL; |
6fc6879b JM |
161 | size_t reply_len = 0; |
162 | int new_attached = 0; | |
163 | ||
164 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
165 | (struct sockaddr *) &from, &fromlen); | |
166 | if (res < 0) { | |
2c6f8cf6 JM |
167 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
168 | strerror(errno)); | |
6fc6879b JM |
169 | return; |
170 | } | |
171 | buf[res] = '\0'; | |
172 | ||
173 | if (os_strcmp(buf, "ATTACH") == 0) { | |
214e428b JM |
174 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, |
175 | fromlen)) | |
6fc6879b JM |
176 | reply_len = 1; |
177 | else { | |
178 | new_attached = 1; | |
179 | reply_len = 2; | |
180 | } | |
181 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
214e428b JM |
182 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, |
183 | fromlen)) | |
6fc6879b JM |
184 | reply_len = 1; |
185 | else | |
186 | reply_len = 2; | |
187 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
188 | if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, | |
189 | buf + 6)) | |
190 | reply_len = 1; | |
191 | else | |
192 | reply_len = 2; | |
193 | } else { | |
742e715b JM |
194 | reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf, |
195 | &reply_len); | |
196 | reply = reply_buf; | |
197 | } | |
198 | ||
199 | if (!reply && reply_len == 1) { | |
200 | reply = "FAIL\n"; | |
201 | reply_len = 5; | |
202 | } else if (!reply && reply_len == 2) { | |
203 | reply = "OK\n"; | |
204 | reply_len = 3; | |
6fc6879b JM |
205 | } |
206 | ||
207 | if (reply) { | |
79986bf6 JM |
208 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
209 | fromlen) < 0) { | |
89286e91 | 210 | int _errno = errno; |
79986bf6 | 211 | wpa_dbg(wpa_s, MSG_DEBUG, |
89286e91 JM |
212 | "ctrl_iface sendto failed: %d - %s", |
213 | _errno, strerror(_errno)); | |
214 | if (_errno == ENOBUFS || _errno == EAGAIN) { | |
215 | /* | |
216 | * The socket send buffer could be full. This | |
217 | * may happen if client programs are not | |
218 | * receiving their pending messages. Close and | |
219 | * reopen the socket as a workaround to avoid | |
220 | * getting stuck being unable to send any new | |
221 | * responses. | |
222 | */ | |
223 | sock = wpas_ctrl_iface_reinit(wpa_s, priv); | |
224 | if (sock < 0) { | |
225 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket"); | |
226 | } | |
227 | } | |
228 | if (new_attached) { | |
229 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching"); | |
230 | new_attached = 0; | |
231 | wpa_supplicant_ctrl_iface_detach( | |
232 | &priv->ctrl_dst, &from, fromlen); | |
233 | } | |
79986bf6 | 234 | } |
6fc6879b | 235 | } |
742e715b | 236 | os_free(reply_buf); |
6fc6879b JM |
237 | |
238 | if (new_attached) | |
239 | eapol_sm_notify_ctrl_attached(wpa_s->eapol); | |
240 | } | |
241 | ||
242 | ||
243 | static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s) | |
244 | { | |
245 | char *buf; | |
246 | size_t len; | |
6ed626df | 247 | char *pbuf, *dir = NULL; |
6fc6879b JM |
248 | int res; |
249 | ||
250 | if (wpa_s->conf->ctrl_interface == NULL) | |
251 | return NULL; | |
252 | ||
253 | pbuf = os_strdup(wpa_s->conf->ctrl_interface); | |
254 | if (pbuf == NULL) | |
255 | return NULL; | |
256 | if (os_strncmp(pbuf, "DIR=", 4) == 0) { | |
6ed626df | 257 | char *gid_str; |
6fc6879b JM |
258 | dir = pbuf + 4; |
259 | gid_str = os_strstr(dir, " GROUP="); | |
6ed626df | 260 | if (gid_str) |
6fc6879b | 261 | *gid_str = '\0'; |
6fc6879b JM |
262 | } else |
263 | dir = pbuf; | |
264 | ||
265 | len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2; | |
266 | buf = os_malloc(len); | |
267 | if (buf == NULL) { | |
268 | os_free(pbuf); | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname); | |
273 | if (res < 0 || (size_t) res >= len) { | |
274 | os_free(pbuf); | |
275 | os_free(buf); | |
276 | return NULL; | |
277 | } | |
278 | #ifdef __CYGWIN__ | |
279 | { | |
280 | /* Windows/WinPcap uses interface names that are not suitable | |
281 | * as a file name - convert invalid chars to underscores */ | |
282 | char *pos = buf; | |
283 | while (*pos) { | |
284 | if (*pos == '\\') | |
285 | *pos = '_'; | |
286 | pos++; | |
287 | } | |
288 | } | |
289 | #endif /* __CYGWIN__ */ | |
290 | os_free(pbuf); | |
291 | return buf; | |
292 | } | |
293 | ||
294 | ||
47bfe49c | 295 | static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global, |
6fc6879b JM |
296 | const char *txt, size_t len) |
297 | { | |
298 | struct wpa_supplicant *wpa_s = ctx; | |
214e428b JM |
299 | |
300 | if (wpa_s == NULL) | |
6fc6879b | 301 | return; |
214e428b | 302 | |
c4bf83a7 | 303 | if (global != 2 && wpa_s->global->ctrl_iface) { |
214e428b JM |
304 | struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface; |
305 | if (!dl_list_empty(&priv->ctrl_dst)) { | |
89286e91 | 306 | wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL : |
47bfe49c | 307 | wpa_s->ifname, |
214e428b JM |
308 | priv->sock, |
309 | &priv->ctrl_dst, | |
89286e91 JM |
310 | level, txt, len, NULL, |
311 | priv); | |
214e428b JM |
312 | } |
313 | } | |
314 | ||
315 | if (wpa_s->ctrl_iface == NULL) | |
316 | return; | |
89286e91 | 317 | wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock, |
214e428b | 318 | &wpa_s->ctrl_iface->ctrl_dst, |
89286e91 JM |
319 | level, txt, len, wpa_s->ctrl_iface, |
320 | NULL); | |
6fc6879b JM |
321 | } |
322 | ||
323 | ||
89286e91 JM |
324 | static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s, |
325 | struct ctrl_iface_priv *priv) | |
6fc6879b | 326 | { |
6fc6879b JM |
327 | struct sockaddr_un addr; |
328 | char *fname = NULL; | |
329 | gid_t gid = 0; | |
330 | int gid_set = 0; | |
331 | char *buf, *dir = NULL, *gid_str = NULL; | |
332 | struct group *grp; | |
333 | char *endp; | |
4fdc8def | 334 | int flags; |
6fc6879b | 335 | |
6fc6879b JM |
336 | buf = os_strdup(wpa_s->conf->ctrl_interface); |
337 | if (buf == NULL) | |
338 | goto fail; | |
b3f3865e DS |
339 | #ifdef ANDROID |
340 | os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s", | |
341 | wpa_s->conf->ctrl_interface); | |
342 | priv->sock = android_get_control_socket(addr.sun_path); | |
343 | if (priv->sock >= 0) | |
344 | goto havesock; | |
345 | #endif /* ANDROID */ | |
6fc6879b JM |
346 | if (os_strncmp(buf, "DIR=", 4) == 0) { |
347 | dir = buf + 4; | |
348 | gid_str = os_strstr(dir, " GROUP="); | |
349 | if (gid_str) { | |
350 | *gid_str = '\0'; | |
351 | gid_str += 7; | |
352 | } | |
353 | } else { | |
354 | dir = buf; | |
355 | gid_str = wpa_s->conf->ctrl_interface_group; | |
356 | } | |
357 | ||
358 | if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) { | |
359 | if (errno == EEXIST) { | |
360 | wpa_printf(MSG_DEBUG, "Using existing control " | |
361 | "interface directory."); | |
362 | } else { | |
2c6f8cf6 JM |
363 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s", |
364 | dir, strerror(errno)); | |
6fc6879b JM |
365 | goto fail; |
366 | } | |
367 | } | |
368 | ||
d49ea682 JM |
369 | #ifdef ANDROID |
370 | /* | |
371 | * wpa_supplicant is started from /init.*.rc on Android and that seems | |
372 | * to be using umask 0077 which would leave the control interface | |
373 | * directory without group access. This breaks things since Wi-Fi | |
374 | * framework assumes that this directory can be accessed by other | |
375 | * applications in the wifi group. Fix this by adding group access even | |
376 | * if umask value would prevent this. | |
377 | */ | |
378 | if (chmod(dir, S_IRWXU | S_IRWXG) < 0) { | |
379 | wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", | |
380 | strerror(errno)); | |
381 | /* Try to continue anyway */ | |
382 | } | |
383 | #endif /* ANDROID */ | |
384 | ||
6fc6879b JM |
385 | if (gid_str) { |
386 | grp = getgrnam(gid_str); | |
387 | if (grp) { | |
388 | gid = grp->gr_gid; | |
389 | gid_set = 1; | |
390 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
391 | " (from group name '%s')", | |
392 | (int) gid, gid_str); | |
393 | } else { | |
394 | /* Group name not found - try to parse this as gid */ | |
395 | gid = strtol(gid_str, &endp, 10); | |
396 | if (*gid_str == '\0' || *endp != '\0') { | |
8e888179 | 397 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " |
6fc6879b JM |
398 | "'%s'", gid_str); |
399 | goto fail; | |
400 | } | |
401 | gid_set = 1; | |
402 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
403 | (int) gid); | |
404 | } | |
405 | } | |
406 | ||
407 | if (gid_set && chown(dir, -1, gid) < 0) { | |
2c6f8cf6 JM |
408 | wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s", |
409 | dir, (int) gid, strerror(errno)); | |
6fc6879b JM |
410 | goto fail; |
411 | } | |
412 | ||
3fd2a226 AAS |
413 | /* Make sure the group can enter and read the directory */ |
414 | if (gid_set && | |
415 | chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) { | |
416 | wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s", | |
417 | strerror(errno)); | |
418 | goto fail; | |
419 | } | |
420 | ||
6fc6879b JM |
421 | if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >= |
422 | sizeof(addr.sun_path)) { | |
423 | wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded"); | |
424 | goto fail; | |
425 | } | |
426 | ||
427 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
428 | if (priv->sock < 0) { | |
2c6f8cf6 | 429 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
6fc6879b JM |
430 | goto fail; |
431 | } | |
432 | ||
433 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 434 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
435 | addr.sun_len = sizeof(addr); |
436 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
437 | addr.sun_family = AF_UNIX; |
438 | fname = wpa_supplicant_ctrl_iface_path(wpa_s); | |
439 | if (fname == NULL) | |
440 | goto fail; | |
441 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
442 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
443 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", | |
444 | strerror(errno)); | |
445 | if (connect(priv->sock, (struct sockaddr *) &addr, | |
446 | sizeof(addr)) < 0) { | |
447 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
448 | " allow connections - assuming it was left" | |
449 | "over from forced program termination"); | |
450 | if (unlink(fname) < 0) { | |
2c6f8cf6 JM |
451 | wpa_printf(MSG_ERROR, |
452 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
453 | fname, strerror(errno)); | |
6fc6879b JM |
454 | goto fail; |
455 | } | |
456 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
457 | sizeof(addr)) < 0) { | |
2c6f8cf6 JM |
458 | wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s", |
459 | strerror(errno)); | |
6fc6879b JM |
460 | goto fail; |
461 | } | |
462 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
463 | "ctrl_iface socket '%s'", fname); | |
464 | } else { | |
465 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
466 | "be in use - cannot override it"); | |
467 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
468 | "not used anymore", fname); | |
469 | os_free(fname); | |
470 | fname = NULL; | |
471 | goto fail; | |
472 | } | |
473 | } | |
474 | ||
475 | if (gid_set && chown(fname, -1, gid) < 0) { | |
2c6f8cf6 JM |
476 | wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s", |
477 | fname, (int) gid, strerror(errno)); | |
6fc6879b JM |
478 | goto fail; |
479 | } | |
480 | ||
481 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { | |
2c6f8cf6 JM |
482 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s", |
483 | fname, strerror(errno)); | |
6fc6879b JM |
484 | goto fail; |
485 | } | |
486 | os_free(fname); | |
487 | ||
b3f3865e DS |
488 | #ifdef ANDROID |
489 | havesock: | |
490 | #endif /* ANDROID */ | |
4fdc8def BG |
491 | |
492 | /* | |
493 | * Make socket non-blocking so that we don't hang forever if | |
494 | * target dies unexpectedly. | |
495 | */ | |
496 | flags = fcntl(priv->sock, F_GETFL); | |
497 | if (flags >= 0) { | |
498 | flags |= O_NONBLOCK; | |
499 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
2c6f8cf6 JM |
500 | wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s", |
501 | strerror(errno)); | |
4fdc8def BG |
502 | /* Not fatal, continue on.*/ |
503 | } | |
504 | } | |
505 | ||
6fc6879b JM |
506 | eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive, |
507 | wpa_s, priv); | |
508 | wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); | |
509 | ||
510 | os_free(buf); | |
89286e91 | 511 | return 0; |
6fc6879b JM |
512 | |
513 | fail: | |
89286e91 | 514 | if (priv->sock >= 0) { |
6fc6879b | 515 | close(priv->sock); |
89286e91 JM |
516 | priv->sock = -1; |
517 | } | |
6fc6879b JM |
518 | if (fname) { |
519 | unlink(fname); | |
520 | os_free(fname); | |
521 | } | |
522 | os_free(buf); | |
89286e91 JM |
523 | return -1; |
524 | } | |
525 | ||
526 | ||
527 | struct ctrl_iface_priv * | |
528 | wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s) | |
529 | { | |
530 | struct ctrl_iface_priv *priv; | |
531 | ||
532 | priv = os_zalloc(sizeof(*priv)); | |
533 | if (priv == NULL) | |
534 | return NULL; | |
535 | dl_list_init(&priv->ctrl_dst); | |
536 | priv->wpa_s = wpa_s; | |
537 | priv->sock = -1; | |
538 | ||
539 | if (wpa_s->conf->ctrl_interface == NULL) | |
540 | return priv; | |
541 | ||
542 | if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) { | |
543 | os_free(priv); | |
544 | return NULL; | |
545 | } | |
546 | ||
547 | return priv; | |
548 | } | |
549 | ||
550 | ||
551 | static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s, | |
552 | struct ctrl_iface_priv *priv) | |
553 | { | |
554 | int res; | |
555 | ||
556 | if (priv->sock <= 0) | |
557 | return -1; | |
558 | ||
559 | eloop_unregister_read_sock(priv->sock); | |
560 | close(priv->sock); | |
561 | priv->sock = -1; | |
562 | res = wpas_ctrl_iface_open_sock(wpa_s, priv); | |
563 | if (res < 0) | |
564 | return -1; | |
565 | return priv->sock; | |
6fc6879b JM |
566 | } |
567 | ||
568 | ||
569 | void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv) | |
570 | { | |
571 | struct wpa_ctrl_dst *dst, *prev; | |
572 | ||
573 | if (priv->sock > -1) { | |
574 | char *fname; | |
6ed626df | 575 | char *buf, *dir = NULL; |
6fc6879b | 576 | eloop_unregister_read_sock(priv->sock); |
09e47a07 | 577 | if (!dl_list_empty(&priv->ctrl_dst)) { |
6fc6879b | 578 | /* |
e0591c3c | 579 | * Wait before closing the control socket if |
6fc6879b JM |
580 | * there are any attached monitors in order to allow |
581 | * them to receive any pending messages. | |
582 | */ | |
583 | wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached " | |
584 | "monitors to receive messages"); | |
e0591c3c | 585 | os_sleep(0, 100000); |
6fc6879b JM |
586 | } |
587 | close(priv->sock); | |
588 | priv->sock = -1; | |
589 | fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s); | |
590 | if (fname) { | |
591 | unlink(fname); | |
592 | os_free(fname); | |
593 | } | |
594 | ||
ea61aa1d JM |
595 | if (priv->wpa_s->conf->ctrl_interface == NULL) |
596 | goto free_dst; | |
6fc6879b JM |
597 | buf = os_strdup(priv->wpa_s->conf->ctrl_interface); |
598 | if (buf == NULL) | |
599 | goto free_dst; | |
600 | if (os_strncmp(buf, "DIR=", 4) == 0) { | |
6ed626df | 601 | char *gid_str; |
6fc6879b JM |
602 | dir = buf + 4; |
603 | gid_str = os_strstr(dir, " GROUP="); | |
6ed626df | 604 | if (gid_str) |
6fc6879b | 605 | *gid_str = '\0'; |
6fc6879b JM |
606 | } else |
607 | dir = buf; | |
608 | ||
609 | if (rmdir(dir) < 0) { | |
610 | if (errno == ENOTEMPTY) { | |
611 | wpa_printf(MSG_DEBUG, "Control interface " | |
612 | "directory not empty - leaving it " | |
613 | "behind"); | |
614 | } else { | |
2c6f8cf6 JM |
615 | wpa_printf(MSG_ERROR, |
616 | "rmdir[ctrl_interface=%s]: %s", | |
617 | dir, strerror(errno)); | |
6fc6879b JM |
618 | } |
619 | } | |
620 | os_free(buf); | |
621 | } | |
622 | ||
623 | free_dst: | |
09e47a07 JM |
624 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
625 | list) | |
626 | os_free(dst); | |
6fc6879b JM |
627 | os_free(priv); |
628 | } | |
629 | ||
630 | ||
631 | /** | |
632 | * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors | |
214e428b JM |
633 | * @ifname: Interface name for global control socket or %NULL |
634 | * @sock: Local socket fd | |
635 | * @ctrl_dst: List of attached listeners | |
6fc6879b JM |
636 | * @level: Priority level of the message |
637 | * @buf: Message data | |
638 | * @len: Message length | |
639 | * | |
640 | * Send a packet to all monitor programs attached to the control interface. | |
641 | */ | |
89286e91 JM |
642 | static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s, |
643 | const char *ifname, int sock, | |
214e428b | 644 | struct dl_list *ctrl_dst, |
6fc6879b | 645 | int level, const char *buf, |
89286e91 JM |
646 | size_t len, |
647 | struct ctrl_iface_priv *priv, | |
648 | struct ctrl_iface_global_priv *gp) | |
6fc6879b JM |
649 | { |
650 | struct wpa_ctrl_dst *dst, *next; | |
651 | char levelstr[10]; | |
652 | int idx, res; | |
653 | struct msghdr msg; | |
214e428b | 654 | struct iovec io[5]; |
6fc6879b | 655 | |
214e428b | 656 | if (sock < 0 || dl_list_empty(ctrl_dst)) |
6fc6879b JM |
657 | return; |
658 | ||
659 | res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
660 | if (res < 0 || (size_t) res >= sizeof(levelstr)) | |
661 | return; | |
214e428b JM |
662 | idx = 0; |
663 | if (ifname) { | |
664 | io[idx].iov_base = "IFNAME="; | |
665 | io[idx].iov_len = 7; | |
666 | idx++; | |
667 | io[idx].iov_base = (char *) ifname; | |
668 | io[idx].iov_len = os_strlen(ifname); | |
669 | idx++; | |
670 | io[idx].iov_base = " "; | |
671 | io[idx].iov_len = 1; | |
672 | idx++; | |
673 | } | |
674 | io[idx].iov_base = levelstr; | |
675 | io[idx].iov_len = os_strlen(levelstr); | |
676 | idx++; | |
677 | io[idx].iov_base = (char *) buf; | |
678 | io[idx].iov_len = len; | |
679 | idx++; | |
6fc6879b JM |
680 | os_memset(&msg, 0, sizeof(msg)); |
681 | msg.msg_iov = io; | |
214e428b | 682 | msg.msg_iovlen = idx; |
6fc6879b | 683 | |
214e428b | 684 | dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { |
89286e91 | 685 | int _errno; |
c9b55597 | 686 | char addr_txt[200]; |
89286e91 | 687 | |
89286e91 JM |
688 | if (level < dst->debug_level) |
689 | continue; | |
690 | ||
c9b55597 JM |
691 | printf_encode(addr_txt, sizeof(addr_txt), |
692 | (u8 *) dst->addr.sun_path, dst->addrlen - | |
693 | offsetof(struct sockaddr_un, sun_path)); | |
89286e91 JM |
694 | msg.msg_name = (void *) &dst->addr; |
695 | msg.msg_namelen = dst->addrlen; | |
696 | if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) { | |
c9b55597 JM |
697 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor sent successfully to %s", |
698 | addr_txt); | |
89286e91 | 699 | dst->errors = 0; |
89286e91 JM |
700 | continue; |
701 | } | |
702 | ||
703 | _errno = errno; | |
c9b55597 JM |
704 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s", |
705 | addr_txt, errno, strerror(errno)); | |
89286e91 JM |
706 | dst->errors++; |
707 | ||
708 | if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) { | |
c9b55597 JM |
709 | wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages", |
710 | addr_txt); | |
89286e91 JM |
711 | wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr, |
712 | dst->addrlen); | |
713 | } | |
714 | ||
715 | if (_errno == ENOBUFS || _errno == EAGAIN) { | |
716 | /* | |
717 | * The socket send buffer could be full. This may happen | |
718 | * if client programs are not receiving their pending | |
719 | * messages. Close and reopen the socket as a workaround | |
720 | * to avoid getting stuck being unable to send any new | |
721 | * responses. | |
722 | */ | |
723 | if (priv) | |
724 | sock = wpas_ctrl_iface_reinit(wpa_s, priv); | |
725 | else if (gp) | |
726 | sock = wpas_ctrl_iface_global_reinit( | |
727 | wpa_s->global, gp); | |
728 | else | |
729 | break; | |
730 | if (sock < 0) { | |
731 | wpa_dbg(wpa_s, MSG_DEBUG, | |
732 | "Failed to reinitialize ctrl_iface socket"); | |
29179b88 | 733 | break; |
89286e91 JM |
734 | } |
735 | } | |
6fc6879b JM |
736 | } |
737 | } | |
738 | ||
739 | ||
740 | void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) | |
741 | { | |
742 | char buf[256]; | |
743 | int res; | |
744 | struct sockaddr_un from; | |
745 | socklen_t fromlen = sizeof(from); | |
746 | ||
747 | for (;;) { | |
748 | wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to " | |
749 | "attach", priv->wpa_s->ifname); | |
750 | eloop_wait_for_read_sock(priv->sock); | |
751 | ||
752 | res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0, | |
753 | (struct sockaddr *) &from, &fromlen); | |
754 | if (res < 0) { | |
2c6f8cf6 JM |
755 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
756 | strerror(errno)); | |
6fc6879b JM |
757 | continue; |
758 | } | |
759 | buf[res] = '\0'; | |
760 | ||
761 | if (os_strcmp(buf, "ATTACH") == 0) { | |
762 | /* handle ATTACH signal of first monitor interface */ | |
214e428b JM |
763 | if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, |
764 | &from, fromlen)) { | |
79986bf6 JM |
765 | if (sendto(priv->sock, "OK\n", 3, 0, |
766 | (struct sockaddr *) &from, fromlen) < | |
767 | 0) { | |
768 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
769 | strerror(errno)); | |
770 | } | |
6fc6879b JM |
771 | /* OK to continue */ |
772 | return; | |
773 | } else { | |
79986bf6 JM |
774 | if (sendto(priv->sock, "FAIL\n", 5, 0, |
775 | (struct sockaddr *) &from, fromlen) < | |
776 | 0) { | |
777 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
778 | strerror(errno)); | |
779 | } | |
6fc6879b JM |
780 | } |
781 | } else { | |
782 | /* return FAIL for all other signals */ | |
79986bf6 JM |
783 | if (sendto(priv->sock, "FAIL\n", 5, 0, |
784 | (struct sockaddr *) &from, fromlen) < 0) { | |
785 | wpa_printf(MSG_DEBUG, | |
786 | "ctrl_iface sendto failed: %s", | |
787 | strerror(errno)); | |
788 | } | |
6fc6879b JM |
789 | } |
790 | } | |
791 | } | |
792 | ||
793 | ||
794 | /* Global ctrl_iface */ | |
795 | ||
6fc6879b JM |
796 | static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx, |
797 | void *sock_ctx) | |
798 | { | |
799 | struct wpa_global *global = eloop_ctx; | |
214e428b | 800 | struct ctrl_iface_global_priv *priv = sock_ctx; |
8615bdfa | 801 | char buf[4096]; |
6fc6879b JM |
802 | int res; |
803 | struct sockaddr_un from; | |
804 | socklen_t fromlen = sizeof(from); | |
742e715b | 805 | char *reply = NULL, *reply_buf = NULL; |
6fc6879b JM |
806 | size_t reply_len; |
807 | ||
808 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
809 | (struct sockaddr *) &from, &fromlen); | |
810 | if (res < 0) { | |
2c6f8cf6 JM |
811 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
812 | strerror(errno)); | |
6fc6879b JM |
813 | return; |
814 | } | |
815 | buf[res] = '\0'; | |
816 | ||
214e428b JM |
817 | if (os_strcmp(buf, "ATTACH") == 0) { |
818 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, | |
819 | fromlen)) | |
820 | reply_len = 1; | |
821 | else | |
822 | reply_len = 2; | |
823 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
824 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, | |
825 | fromlen)) | |
826 | reply_len = 1; | |
827 | else | |
828 | reply_len = 2; | |
829 | } else { | |
742e715b JM |
830 | reply_buf = wpa_supplicant_global_ctrl_iface_process( |
831 | global, buf, &reply_len); | |
832 | reply = reply_buf; | |
833 | } | |
834 | ||
835 | if (!reply && reply_len == 1) { | |
836 | reply = "FAIL\n"; | |
837 | reply_len = 5; | |
838 | } else if (!reply && reply_len == 2) { | |
839 | reply = "OK\n"; | |
840 | reply_len = 3; | |
214e428b | 841 | } |
6fc6879b JM |
842 | |
843 | if (reply) { | |
79986bf6 JM |
844 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
845 | fromlen) < 0) { | |
846 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
847 | strerror(errno)); | |
848 | } | |
6fc6879b | 849 | } |
742e715b | 850 | os_free(reply_buf); |
6fc6879b JM |
851 | } |
852 | ||
853 | ||
89286e91 JM |
854 | static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global, |
855 | struct ctrl_iface_global_priv *priv) | |
6fc6879b | 856 | { |
6fc6879b | 857 | struct sockaddr_un addr; |
d2a9e2c7 | 858 | const char *ctrl = global->params.ctrl_interface; |
8d6e0350 | 859 | int flags; |
6fc6879b | 860 | |
d2a9e2c7 JM |
861 | wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl); |
862 | ||
b3f3865e | 863 | #ifdef ANDROID |
d2a9e2c7 JM |
864 | if (os_strncmp(ctrl, "@android:", 9) == 0) { |
865 | priv->sock = android_get_control_socket(ctrl + 9); | |
866 | if (priv->sock < 0) { | |
867 | wpa_printf(MSG_ERROR, "Failed to open Android control " | |
868 | "socket '%s'", ctrl + 9); | |
869 | goto fail; | |
870 | } | |
871 | wpa_printf(MSG_DEBUG, "Using Android control socket '%s'", | |
872 | ctrl + 9); | |
b3f3865e | 873 | goto havesock; |
d2a9e2c7 | 874 | } |
b3f3865e | 875 | |
d2a9e2c7 JM |
876 | if (os_strncmp(ctrl, "@abstract:", 10) != 0) { |
877 | /* | |
878 | * Backwards compatibility - try to open an Android control | |
879 | * socket and if that fails, assume this was a UNIX domain | |
880 | * socket instead. | |
881 | */ | |
882 | priv->sock = android_get_control_socket(ctrl); | |
883 | if (priv->sock >= 0) { | |
884 | wpa_printf(MSG_DEBUG, | |
885 | "Using Android control socket '%s'", | |
886 | ctrl); | |
887 | goto havesock; | |
888 | } | |
889 | } | |
890 | #endif /* ANDROID */ | |
6fc6879b JM |
891 | |
892 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
893 | if (priv->sock < 0) { | |
2c6f8cf6 | 894 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
6fc6879b JM |
895 | goto fail; |
896 | } | |
897 | ||
898 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 899 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
900 | addr.sun_len = sizeof(addr); |
901 | #endif /* __FreeBSD__ */ | |
6fc6879b | 902 | addr.sun_family = AF_UNIX; |
d2a9e2c7 JM |
903 | |
904 | if (os_strncmp(ctrl, "@abstract:", 10) == 0) { | |
905 | addr.sun_path[0] = '\0'; | |
906 | os_strlcpy(addr.sun_path + 1, ctrl + 10, | |
907 | sizeof(addr.sun_path) - 1); | |
908 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < | |
909 | 0) { | |
910 | wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: " | |
2c6f8cf6 JM |
911 | "bind(PF_UNIX;%s) failed: %s", |
912 | ctrl, strerror(errno)); | |
d2a9e2c7 JM |
913 | goto fail; |
914 | } | |
915 | wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'", | |
916 | ctrl + 10); | |
917 | goto havesock; | |
918 | } | |
919 | ||
920 | os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path)); | |
6fc6879b | 921 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
2c6f8cf6 JM |
922 | wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s", |
923 | ctrl, strerror(errno)); | |
6fc6879b JM |
924 | if (connect(priv->sock, (struct sockaddr *) &addr, |
925 | sizeof(addr)) < 0) { | |
926 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
927 | " allow connections - assuming it was left" | |
928 | "over from forced program termination"); | |
d2a9e2c7 | 929 | if (unlink(ctrl) < 0) { |
2c6f8cf6 JM |
930 | wpa_printf(MSG_ERROR, |
931 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
932 | ctrl, strerror(errno)); | |
6fc6879b JM |
933 | goto fail; |
934 | } | |
935 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
936 | sizeof(addr)) < 0) { | |
2c6f8cf6 JM |
937 | wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s", |
938 | ctrl, strerror(errno)); | |
6fc6879b JM |
939 | goto fail; |
940 | } | |
941 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
942 | "ctrl_iface socket '%s'", | |
d2a9e2c7 | 943 | ctrl); |
6fc6879b JM |
944 | } else { |
945 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
946 | "be in use - cannot override it"); | |
947 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
948 | "not used anymore", | |
d2a9e2c7 | 949 | ctrl); |
6fc6879b JM |
950 | goto fail; |
951 | } | |
952 | } | |
953 | ||
d2a9e2c7 JM |
954 | wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl); |
955 | ||
29257565 JM |
956 | if (global->params.ctrl_interface_group) { |
957 | char *gid_str = global->params.ctrl_interface_group; | |
958 | gid_t gid = 0; | |
959 | struct group *grp; | |
960 | char *endp; | |
961 | ||
962 | grp = getgrnam(gid_str); | |
963 | if (grp) { | |
964 | gid = grp->gr_gid; | |
965 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
966 | " (from group name '%s')", | |
967 | (int) gid, gid_str); | |
968 | } else { | |
969 | /* Group name not found - try to parse this as gid */ | |
970 | gid = strtol(gid_str, &endp, 10); | |
971 | if (*gid_str == '\0' || *endp != '\0') { | |
972 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " | |
973 | "'%s'", gid_str); | |
974 | goto fail; | |
975 | } | |
976 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
977 | (int) gid); | |
978 | } | |
d2a9e2c7 | 979 | if (chown(ctrl, -1, gid) < 0) { |
2c6f8cf6 JM |
980 | wpa_printf(MSG_ERROR, |
981 | "chown[global_ctrl_interface=%s,gid=%d]: %s", | |
982 | ctrl, (int) gid, strerror(errno)); | |
29257565 JM |
983 | goto fail; |
984 | } | |
985 | ||
d2a9e2c7 | 986 | if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) { |
2c6f8cf6 JM |
987 | wpa_printf(MSG_ERROR, |
988 | "chmod[global_ctrl_interface=%s]: %s", | |
989 | ctrl, strerror(errno)); | |
29257565 JM |
990 | goto fail; |
991 | } | |
d2a9e2c7 | 992 | } else { |
f62415df JM |
993 | if (chmod(ctrl, S_IRWXU) < 0) { |
994 | wpa_printf(MSG_DEBUG, | |
995 | "chmod[global_ctrl_interface=%s](S_IRWXU): %s", | |
996 | ctrl, strerror(errno)); | |
997 | /* continue anyway since group change was not required | |
998 | */ | |
999 | } | |
29257565 JM |
1000 | } |
1001 | ||
b3f3865e | 1002 | havesock: |
8d6e0350 JM |
1003 | |
1004 | /* | |
1005 | * Make socket non-blocking so that we don't hang forever if | |
1006 | * target dies unexpectedly. | |
1007 | */ | |
1008 | flags = fcntl(priv->sock, F_GETFL); | |
1009 | if (flags >= 0) { | |
1010 | flags |= O_NONBLOCK; | |
1011 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
2c6f8cf6 JM |
1012 | wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s", |
1013 | strerror(errno)); | |
8d6e0350 JM |
1014 | /* Not fatal, continue on.*/ |
1015 | } | |
1016 | } | |
1017 | ||
6fc6879b JM |
1018 | eloop_register_read_sock(priv->sock, |
1019 | wpa_supplicant_global_ctrl_iface_receive, | |
214e428b | 1020 | global, priv); |
6fc6879b | 1021 | |
89286e91 | 1022 | return 0; |
6fc6879b JM |
1023 | |
1024 | fail: | |
89286e91 | 1025 | if (priv->sock >= 0) { |
6fc6879b | 1026 | close(priv->sock); |
89286e91 JM |
1027 | priv->sock = -1; |
1028 | } | |
1029 | return -1; | |
1030 | } | |
1031 | ||
1032 | ||
1033 | struct ctrl_iface_global_priv * | |
1034 | wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global) | |
1035 | { | |
1036 | struct ctrl_iface_global_priv *priv; | |
1037 | ||
1038 | priv = os_zalloc(sizeof(*priv)); | |
1039 | if (priv == NULL) | |
1040 | return NULL; | |
1041 | dl_list_init(&priv->ctrl_dst); | |
1042 | priv->global = global; | |
1043 | priv->sock = -1; | |
1044 | ||
1045 | if (global->params.ctrl_interface == NULL) | |
1046 | return priv; | |
1047 | ||
1048 | if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) { | |
1049 | os_free(priv); | |
1050 | return NULL; | |
1051 | } | |
1052 | ||
76fe79ef JM |
1053 | wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); |
1054 | ||
89286e91 JM |
1055 | return priv; |
1056 | } | |
1057 | ||
1058 | ||
1059 | static int wpas_ctrl_iface_global_reinit(struct wpa_global *global, | |
1060 | struct ctrl_iface_global_priv *priv) | |
1061 | { | |
1062 | int res; | |
1063 | ||
1064 | if (priv->sock <= 0) | |
1065 | return -1; | |
1066 | ||
1067 | eloop_unregister_read_sock(priv->sock); | |
1068 | close(priv->sock); | |
1069 | priv->sock = -1; | |
1070 | res = wpas_global_ctrl_iface_open_sock(global, priv); | |
1071 | if (res < 0) | |
1072 | return -1; | |
1073 | return priv->sock; | |
6fc6879b JM |
1074 | } |
1075 | ||
1076 | ||
1077 | void | |
1078 | wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv) | |
1079 | { | |
214e428b JM |
1080 | struct wpa_ctrl_dst *dst, *prev; |
1081 | ||
6fc6879b JM |
1082 | if (priv->sock >= 0) { |
1083 | eloop_unregister_read_sock(priv->sock); | |
1084 | close(priv->sock); | |
1085 | } | |
1086 | if (priv->global->params.ctrl_interface) | |
1087 | unlink(priv->global->params.ctrl_interface); | |
214e428b JM |
1088 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
1089 | list) | |
1090 | os_free(dst); | |
6fc6879b JM |
1091 | os_free(priv); |
1092 | } |