]>
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> | |
2e95cfc1 JM |
16 | #ifdef __linux__ |
17 | #include <sys/ioctl.h> | |
18 | #include <linux/sockios.h> | |
19 | #endif /* __linux__ */ | |
b3f3865e DS |
20 | #ifdef ANDROID |
21 | #include <cutils/sockets.h> | |
22 | #endif /* ANDROID */ | |
6fc6879b | 23 | |
09e47a07 JM |
24 | #include "utils/common.h" |
25 | #include "utils/eloop.h" | |
26 | #include "utils/list.h" | |
6fc6879b | 27 | #include "eapol_supp/eapol_supp_sm.h" |
09e47a07 | 28 | #include "config.h" |
6fc6879b JM |
29 | #include "wpa_supplicant_i.h" |
30 | #include "ctrl_iface.h" | |
31 | ||
32 | /* Per-interface ctrl_iface */ | |
33 | ||
34 | /** | |
35 | * struct wpa_ctrl_dst - Internal data structure of control interface monitors | |
36 | * | |
37 | * This structure is used to store information about registered control | |
38 | * interface monitors into struct wpa_supplicant. This data is private to | |
39 | * ctrl_iface_unix.c and should not be touched directly from other files. | |
40 | */ | |
41 | struct wpa_ctrl_dst { | |
09e47a07 | 42 | struct dl_list list; |
6fc6879b JM |
43 | struct sockaddr_un addr; |
44 | socklen_t addrlen; | |
45 | int debug_level; | |
46 | int errors; | |
47 | }; | |
48 | ||
49 | ||
50 | struct ctrl_iface_priv { | |
51 | struct wpa_supplicant *wpa_s; | |
52 | int sock; | |
09e47a07 | 53 | struct dl_list ctrl_dst; |
3a7414b6 | 54 | int android_control_socket; |
3fdaaa8f JM |
55 | struct dl_list msg_queue; |
56 | unsigned int throttle_count; | |
6fc6879b JM |
57 | }; |
58 | ||
59 | ||
214e428b JM |
60 | struct ctrl_iface_global_priv { |
61 | struct wpa_global *global; | |
62 | int sock; | |
63 | struct dl_list ctrl_dst; | |
3a7414b6 | 64 | int android_control_socket; |
3fdaaa8f JM |
65 | struct dl_list msg_queue; |
66 | unsigned int throttle_count; | |
67 | }; | |
68 | ||
69 | struct ctrl_iface_msg { | |
70 | struct dl_list list; | |
71 | struct wpa_supplicant *wpa_s; | |
72 | int level; | |
73 | enum wpa_msg_type type; | |
74 | const char *txt; | |
75 | size_t len; | |
214e428b JM |
76 | }; |
77 | ||
78 | ||
89286e91 JM |
79 | static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s, |
80 | const char *ifname, int sock, | |
214e428b | 81 | struct dl_list *ctrl_dst, |
6fc6879b | 82 | int level, const char *buf, |
89286e91 JM |
83 | size_t len, |
84 | struct ctrl_iface_priv *priv, | |
85 | struct ctrl_iface_global_priv *gp); | |
86 | static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s, | |
87 | struct ctrl_iface_priv *priv); | |
88 | static int wpas_ctrl_iface_global_reinit(struct wpa_global *global, | |
89 | struct ctrl_iface_global_priv *priv); | |
6fc6879b JM |
90 | |
91 | ||
2e95cfc1 JM |
92 | static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf, |
93 | size_t len) | |
94 | { | |
95 | #ifdef __linux__ | |
96 | socklen_t optlen; | |
97 | int sndbuf, outq; | |
43fa110b | 98 | int level = MSG_MSGDUMP; |
2e95cfc1 JM |
99 | |
100 | if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0) | |
101 | level = MSG_EXCESSIVE; | |
102 | ||
103 | optlen = sizeof(sndbuf); | |
104 | sndbuf = 0; | |
105 | if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0) | |
106 | sndbuf = -1; | |
107 | ||
108 | if (ioctl(sock, SIOCOUTQ, &outq) < 0) | |
109 | outq = -1; | |
110 | ||
111 | wpa_printf(level, | |
112 | "CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d", | |
113 | title, sock, sndbuf, outq, (int) len); | |
114 | #endif /* __linux__ */ | |
115 | } | |
116 | ||
117 | ||
214e428b | 118 | static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst, |
6fc6879b | 119 | struct sockaddr_un *from, |
0efcad2c | 120 | socklen_t fromlen, int global) |
6fc6879b JM |
121 | { |
122 | struct wpa_ctrl_dst *dst; | |
c9b55597 | 123 | char addr_txt[200]; |
6fc6879b JM |
124 | |
125 | dst = os_zalloc(sizeof(*dst)); | |
126 | if (dst == NULL) | |
127 | return -1; | |
128 | os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); | |
129 | dst->addrlen = fromlen; | |
130 | dst->debug_level = MSG_INFO; | |
214e428b | 131 | dl_list_add(ctrl_dst, &dst->list); |
c9b55597 JM |
132 | printf_encode(addr_txt, sizeof(addr_txt), |
133 | (u8 *) from->sun_path, | |
134 | fromlen - offsetof(struct sockaddr_un, sun_path)); | |
0efcad2c JM |
135 | wpa_printf(MSG_DEBUG, "CTRL_IFACE %smonitor attached %s", |
136 | global ? "global " : "", addr_txt); | |
6fc6879b JM |
137 | return 0; |
138 | } | |
139 | ||
140 | ||
214e428b | 141 | static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst, |
6fc6879b JM |
142 | struct sockaddr_un *from, |
143 | socklen_t fromlen) | |
144 | { | |
09e47a07 | 145 | struct wpa_ctrl_dst *dst; |
6fc6879b | 146 | |
214e428b | 147 | dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
148 | if (fromlen == dst->addrlen && |
149 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
150 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
151 | == 0) { | |
c9b55597 JM |
152 | char addr_txt[200]; |
153 | printf_encode(addr_txt, sizeof(addr_txt), | |
154 | (u8 *) from->sun_path, | |
155 | fromlen - | |
156 | offsetof(struct sockaddr_un, sun_path)); | |
157 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s", | |
158 | addr_txt); | |
a235aca3 JM |
159 | dl_list_del(&dst->list); |
160 | os_free(dst); | |
6fc6879b JM |
161 | return 0; |
162 | } | |
6fc6879b JM |
163 | } |
164 | return -1; | |
165 | } | |
166 | ||
167 | ||
168 | static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv, | |
169 | struct sockaddr_un *from, | |
170 | socklen_t fromlen, | |
171 | char *level) | |
172 | { | |
173 | struct wpa_ctrl_dst *dst; | |
174 | ||
175 | wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); | |
176 | ||
09e47a07 | 177 | dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
178 | if (fromlen == dst->addrlen && |
179 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
180 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
181 | == 0) { | |
c9b55597 | 182 | char addr_txt[200]; |
6fc6879b | 183 | dst->debug_level = atoi(level); |
c9b55597 JM |
184 | printf_encode(addr_txt, sizeof(addr_txt), |
185 | (u8 *) from->sun_path, fromlen - | |
186 | offsetof(struct sockaddr_un, sun_path)); | |
187 | wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s", | |
188 | dst->debug_level, addr_txt); | |
6fc6879b JM |
189 | return 0; |
190 | } | |
6fc6879b JM |
191 | } |
192 | ||
193 | return -1; | |
194 | } | |
195 | ||
196 | ||
197 | static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, | |
198 | void *sock_ctx) | |
199 | { | |
200 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
201 | struct ctrl_iface_priv *priv = sock_ctx; | |
b563b388 | 202 | char buf[4096]; |
6fc6879b JM |
203 | int res; |
204 | struct sockaddr_un from; | |
205 | socklen_t fromlen = sizeof(from); | |
742e715b | 206 | char *reply = NULL, *reply_buf = NULL; |
6fc6879b JM |
207 | size_t reply_len = 0; |
208 | int new_attached = 0; | |
209 | ||
210 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
211 | (struct sockaddr *) &from, &fromlen); | |
212 | if (res < 0) { | |
2c6f8cf6 JM |
213 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
214 | strerror(errno)); | |
6fc6879b JM |
215 | return; |
216 | } | |
217 | buf[res] = '\0'; | |
218 | ||
219 | if (os_strcmp(buf, "ATTACH") == 0) { | |
214e428b | 220 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, |
0efcad2c | 221 | fromlen, 0)) |
6fc6879b JM |
222 | reply_len = 1; |
223 | else { | |
224 | new_attached = 1; | |
225 | reply_len = 2; | |
226 | } | |
227 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
214e428b JM |
228 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, |
229 | fromlen)) | |
6fc6879b JM |
230 | reply_len = 1; |
231 | else | |
232 | reply_len = 2; | |
233 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
234 | if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, | |
235 | buf + 6)) | |
236 | reply_len = 1; | |
237 | else | |
238 | reply_len = 2; | |
239 | } else { | |
742e715b JM |
240 | reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf, |
241 | &reply_len); | |
242 | reply = reply_buf; | |
14fd0331 JM |
243 | |
244 | /* | |
245 | * There could be some password/key material in the command, so | |
246 | * clear the buffer explicitly now that it is not needed | |
247 | * anymore. | |
248 | */ | |
249 | os_memset(buf, 0, res); | |
742e715b JM |
250 | } |
251 | ||
252 | if (!reply && reply_len == 1) { | |
253 | reply = "FAIL\n"; | |
254 | reply_len = 5; | |
255 | } else if (!reply && reply_len == 2) { | |
256 | reply = "OK\n"; | |
257 | reply_len = 3; | |
6fc6879b JM |
258 | } |
259 | ||
260 | if (reply) { | |
2e95cfc1 JM |
261 | wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply, |
262 | reply_len); | |
79986bf6 JM |
263 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
264 | fromlen) < 0) { | |
89286e91 | 265 | int _errno = errno; |
79986bf6 | 266 | wpa_dbg(wpa_s, MSG_DEBUG, |
89286e91 JM |
267 | "ctrl_iface sendto failed: %d - %s", |
268 | _errno, strerror(_errno)); | |
269 | if (_errno == ENOBUFS || _errno == EAGAIN) { | |
270 | /* | |
271 | * The socket send buffer could be full. This | |
272 | * may happen if client programs are not | |
273 | * receiving their pending messages. Close and | |
274 | * reopen the socket as a workaround to avoid | |
275 | * getting stuck being unable to send any new | |
276 | * responses. | |
277 | */ | |
278 | sock = wpas_ctrl_iface_reinit(wpa_s, priv); | |
279 | if (sock < 0) { | |
280 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket"); | |
281 | } | |
282 | } | |
283 | if (new_attached) { | |
284 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching"); | |
285 | new_attached = 0; | |
286 | wpa_supplicant_ctrl_iface_detach( | |
287 | &priv->ctrl_dst, &from, fromlen); | |
288 | } | |
79986bf6 | 289 | } |
6fc6879b | 290 | } |
742e715b | 291 | os_free(reply_buf); |
6fc6879b JM |
292 | |
293 | if (new_attached) | |
294 | eapol_sm_notify_ctrl_attached(wpa_s->eapol); | |
295 | } | |
296 | ||
297 | ||
298 | static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s) | |
299 | { | |
300 | char *buf; | |
301 | size_t len; | |
6ed626df | 302 | char *pbuf, *dir = NULL; |
6fc6879b JM |
303 | int res; |
304 | ||
305 | if (wpa_s->conf->ctrl_interface == NULL) | |
306 | return NULL; | |
307 | ||
308 | pbuf = os_strdup(wpa_s->conf->ctrl_interface); | |
309 | if (pbuf == NULL) | |
310 | return NULL; | |
311 | if (os_strncmp(pbuf, "DIR=", 4) == 0) { | |
6ed626df | 312 | char *gid_str; |
6fc6879b JM |
313 | dir = pbuf + 4; |
314 | gid_str = os_strstr(dir, " GROUP="); | |
6ed626df | 315 | if (gid_str) |
6fc6879b | 316 | *gid_str = '\0'; |
6fc6879b JM |
317 | } else |
318 | dir = pbuf; | |
319 | ||
320 | len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2; | |
321 | buf = os_malloc(len); | |
322 | if (buf == NULL) { | |
323 | os_free(pbuf); | |
324 | return NULL; | |
325 | } | |
326 | ||
327 | res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname); | |
d85e1fc8 | 328 | if (os_snprintf_error(len, res)) { |
6fc6879b JM |
329 | os_free(pbuf); |
330 | os_free(buf); | |
331 | return NULL; | |
332 | } | |
333 | #ifdef __CYGWIN__ | |
334 | { | |
335 | /* Windows/WinPcap uses interface names that are not suitable | |
336 | * as a file name - convert invalid chars to underscores */ | |
337 | char *pos = buf; | |
338 | while (*pos) { | |
339 | if (*pos == '\\') | |
340 | *pos = '_'; | |
341 | pos++; | |
342 | } | |
343 | } | |
344 | #endif /* __CYGWIN__ */ | |
345 | os_free(pbuf); | |
346 | return buf; | |
347 | } | |
348 | ||
349 | ||
3fdaaa8f JM |
350 | static int wpas_ctrl_iface_throttle(int sock) |
351 | { | |
352 | #ifdef __linux__ | |
353 | socklen_t optlen; | |
354 | int sndbuf, outq; | |
355 | ||
356 | optlen = sizeof(sndbuf); | |
357 | sndbuf = 0; | |
358 | if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0 || | |
359 | ioctl(sock, SIOCOUTQ, &outq) < 0 || | |
360 | sndbuf <= 0 || outq < 0) | |
361 | return 0; | |
362 | return outq > sndbuf / 2; | |
363 | #else /* __linux__ */ | |
364 | return 0; | |
365 | #endif /* __linux__ */ | |
366 | } | |
367 | ||
368 | ||
369 | static void wpas_ctrl_msg_send_pending_global(struct wpa_global *global) | |
370 | { | |
371 | struct ctrl_iface_global_priv *gpriv; | |
372 | struct ctrl_iface_msg *msg; | |
373 | ||
374 | gpriv = global->ctrl_iface; | |
375 | while (gpriv && !dl_list_empty(&gpriv->msg_queue) && | |
376 | !wpas_ctrl_iface_throttle(gpriv->sock)) { | |
377 | msg = dl_list_first(&gpriv->msg_queue, struct ctrl_iface_msg, | |
378 | list); | |
379 | if (!msg) | |
380 | break; | |
381 | dl_list_del(&msg->list); | |
382 | wpa_supplicant_ctrl_iface_send( | |
383 | msg->wpa_s, | |
384 | msg->type != WPA_MSG_PER_INTERFACE ? | |
385 | NULL : msg->wpa_s->ifname, | |
386 | gpriv->sock, &gpriv->ctrl_dst, msg->level, | |
387 | msg->txt, msg->len, NULL, gpriv); | |
388 | os_free(msg); | |
389 | } | |
390 | } | |
391 | ||
392 | ||
393 | static void wpas_ctrl_msg_send_pending_iface(struct wpa_supplicant *wpa_s) | |
394 | { | |
395 | struct ctrl_iface_priv *priv; | |
396 | struct ctrl_iface_msg *msg; | |
397 | ||
398 | priv = wpa_s->ctrl_iface; | |
399 | while (priv && !dl_list_empty(&priv->msg_queue) && | |
400 | !wpas_ctrl_iface_throttle(priv->sock)) { | |
401 | msg = dl_list_first(&priv->msg_queue, struct ctrl_iface_msg, | |
402 | list); | |
403 | if (!msg) | |
404 | break; | |
405 | dl_list_del(&msg->list); | |
406 | wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock, | |
407 | &priv->ctrl_dst, msg->level, | |
408 | msg->txt, msg->len, priv, NULL); | |
409 | os_free(msg); | |
410 | } | |
411 | } | |
412 | ||
413 | ||
414 | static void wpas_ctrl_msg_queue_timeout(void *eloop_ctx, void *timeout_ctx) | |
415 | { | |
416 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
417 | struct ctrl_iface_priv *priv; | |
418 | struct ctrl_iface_global_priv *gpriv; | |
419 | int sock = -1, gsock = -1; | |
420 | ||
421 | wpas_ctrl_msg_send_pending_global(wpa_s->global); | |
422 | wpas_ctrl_msg_send_pending_iface(wpa_s); | |
423 | ||
424 | priv = wpa_s->ctrl_iface; | |
425 | if (priv && !dl_list_empty(&priv->msg_queue)) | |
426 | sock = priv->sock; | |
427 | ||
428 | gpriv = wpa_s->global->ctrl_iface; | |
429 | if (gpriv && !dl_list_empty(&gpriv->msg_queue)) | |
430 | gsock = gpriv->sock; | |
431 | ||
432 | if (sock > -1 || gsock > -1) { | |
433 | /* Continue pending message transmission from a timeout */ | |
434 | wpa_printf(MSG_MSGDUMP, | |
435 | "CTRL: Had to throttle pending event message transmission for (sock %d gsock %d)", | |
436 | sock, gsock); | |
437 | eloop_register_timeout(0, 20000, wpas_ctrl_msg_queue_timeout, | |
438 | wpa_s, NULL); | |
439 | } | |
440 | } | |
441 | ||
442 | ||
443 | static void wpas_ctrl_msg_queue(struct dl_list *queue, | |
444 | struct wpa_supplicant *wpa_s, int level, | |
445 | enum wpa_msg_type type, | |
446 | const char *txt, size_t len) | |
447 | { | |
448 | struct ctrl_iface_msg *msg; | |
449 | ||
450 | msg = os_zalloc(sizeof(*msg) + len); | |
451 | if (!msg) | |
452 | return; | |
453 | ||
454 | msg->wpa_s = wpa_s; | |
455 | msg->level = level; | |
456 | msg->type = type; | |
457 | os_memcpy(msg + 1, txt, len); | |
458 | msg->txt = (const char *) (msg + 1); | |
459 | msg->len = len; | |
460 | dl_list_add_tail(queue, &msg->list); | |
461 | eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL); | |
462 | eloop_register_timeout(0, 0, wpas_ctrl_msg_queue_timeout, wpa_s, NULL); | |
463 | } | |
464 | ||
465 | ||
466 | static void wpas_ctrl_msg_queue_limit(unsigned int throttle_count, | |
467 | struct dl_list *queue) | |
468 | { | |
469 | struct ctrl_iface_msg *msg; | |
470 | ||
471 | if (throttle_count < 2000) | |
472 | return; | |
473 | ||
474 | msg = dl_list_first(queue, struct ctrl_iface_msg, list); | |
475 | if (msg) { | |
476 | wpa_printf(MSG_DEBUG, "CTRL: Dropped oldest pending message"); | |
477 | dl_list_del(&msg->list); | |
478 | os_free(msg); | |
479 | } | |
480 | } | |
481 | ||
482 | ||
995a3a06 JM |
483 | static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, |
484 | enum wpa_msg_type type, | |
6fc6879b JM |
485 | const char *txt, size_t len) |
486 | { | |
487 | struct wpa_supplicant *wpa_s = ctx; | |
3fdaaa8f JM |
488 | struct ctrl_iface_priv *priv; |
489 | struct ctrl_iface_global_priv *gpriv; | |
214e428b JM |
490 | |
491 | if (wpa_s == NULL) | |
6fc6879b | 492 | return; |
214e428b | 493 | |
3fdaaa8f JM |
494 | gpriv = wpa_s->global->ctrl_iface; |
495 | ||
496 | if (type != WPA_MSG_NO_GLOBAL && gpriv && | |
497 | !dl_list_empty(&gpriv->ctrl_dst)) { | |
498 | if (!dl_list_empty(&gpriv->msg_queue) || | |
499 | wpas_ctrl_iface_throttle(gpriv->sock)) { | |
500 | if (gpriv->throttle_count == 0) { | |
501 | wpa_printf(MSG_MSGDUMP, | |
502 | "CTRL: Had to throttle global event message for sock %d", | |
503 | gpriv->sock); | |
504 | } | |
505 | gpriv->throttle_count++; | |
506 | wpas_ctrl_msg_queue_limit(gpriv->throttle_count, | |
507 | &gpriv->msg_queue); | |
508 | wpas_ctrl_msg_queue(&gpriv->msg_queue, wpa_s, level, | |
509 | type, txt, len); | |
510 | } else { | |
511 | if (gpriv->throttle_count) { | |
512 | wpa_printf(MSG_MSGDUMP, | |
513 | "CTRL: Had to throttle %u global event message(s) for sock %d", | |
514 | gpriv->throttle_count, gpriv->sock); | |
515 | } | |
516 | gpriv->throttle_count = 0; | |
995a3a06 JM |
517 | wpa_supplicant_ctrl_iface_send( |
518 | wpa_s, | |
ee1e3f57 AN |
519 | type != WPA_MSG_PER_INTERFACE ? |
520 | NULL : wpa_s->ifname, | |
3fdaaa8f JM |
521 | gpriv->sock, &gpriv->ctrl_dst, level, |
522 | txt, len, NULL, gpriv); | |
214e428b JM |
523 | } |
524 | } | |
525 | ||
3fdaaa8f JM |
526 | priv = wpa_s->ctrl_iface; |
527 | ||
528 | if (type != WPA_MSG_ONLY_GLOBAL && priv) { | |
529 | if (!dl_list_empty(&priv->msg_queue) || | |
530 | wpas_ctrl_iface_throttle(priv->sock)) { | |
531 | if (priv->throttle_count == 0) { | |
532 | wpa_printf(MSG_MSGDUMP, | |
533 | "CTRL: Had to throttle event message for sock %d", | |
534 | priv->sock); | |
535 | } | |
536 | priv->throttle_count++; | |
537 | wpas_ctrl_msg_queue_limit(priv->throttle_count, | |
538 | &priv->msg_queue); | |
539 | wpas_ctrl_msg_queue(&priv->msg_queue, wpa_s, level, | |
540 | type, txt, len); | |
541 | } else { | |
542 | if (priv->throttle_count) { | |
543 | wpa_printf(MSG_MSGDUMP, | |
544 | "CTRL: Had to throttle %u event message(s) for sock %d", | |
545 | priv->throttle_count, priv->sock); | |
546 | } | |
547 | priv->throttle_count = 0; | |
548 | wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock, | |
549 | &priv->ctrl_dst, level, | |
550 | txt, len, priv, NULL); | |
551 | } | |
552 | } | |
6fc6879b JM |
553 | } |
554 | ||
555 | ||
89286e91 JM |
556 | static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s, |
557 | struct ctrl_iface_priv *priv) | |
6fc6879b | 558 | { |
6fc6879b JM |
559 | struct sockaddr_un addr; |
560 | char *fname = NULL; | |
561 | gid_t gid = 0; | |
562 | int gid_set = 0; | |
563 | char *buf, *dir = NULL, *gid_str = NULL; | |
564 | struct group *grp; | |
565 | char *endp; | |
4fdc8def | 566 | int flags; |
6fc6879b | 567 | |
6fc6879b JM |
568 | buf = os_strdup(wpa_s->conf->ctrl_interface); |
569 | if (buf == NULL) | |
570 | goto fail; | |
b3f3865e DS |
571 | #ifdef ANDROID |
572 | os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s", | |
573 | wpa_s->conf->ctrl_interface); | |
574 | priv->sock = android_get_control_socket(addr.sun_path); | |
3a7414b6 NM |
575 | if (priv->sock >= 0) { |
576 | priv->android_control_socket = 1; | |
b3f3865e | 577 | goto havesock; |
3a7414b6 | 578 | } |
b3f3865e | 579 | #endif /* ANDROID */ |
6fc6879b JM |
580 | if (os_strncmp(buf, "DIR=", 4) == 0) { |
581 | dir = buf + 4; | |
582 | gid_str = os_strstr(dir, " GROUP="); | |
583 | if (gid_str) { | |
584 | *gid_str = '\0'; | |
585 | gid_str += 7; | |
586 | } | |
587 | } else { | |
588 | dir = buf; | |
589 | gid_str = wpa_s->conf->ctrl_interface_group; | |
590 | } | |
591 | ||
592 | if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) { | |
593 | if (errno == EEXIST) { | |
594 | wpa_printf(MSG_DEBUG, "Using existing control " | |
595 | "interface directory."); | |
596 | } else { | |
2c6f8cf6 JM |
597 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s", |
598 | dir, strerror(errno)); | |
6fc6879b JM |
599 | goto fail; |
600 | } | |
601 | } | |
602 | ||
d49ea682 JM |
603 | #ifdef ANDROID |
604 | /* | |
605 | * wpa_supplicant is started from /init.*.rc on Android and that seems | |
606 | * to be using umask 0077 which would leave the control interface | |
607 | * directory without group access. This breaks things since Wi-Fi | |
608 | * framework assumes that this directory can be accessed by other | |
609 | * applications in the wifi group. Fix this by adding group access even | |
610 | * if umask value would prevent this. | |
611 | */ | |
612 | if (chmod(dir, S_IRWXU | S_IRWXG) < 0) { | |
613 | wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", | |
614 | strerror(errno)); | |
615 | /* Try to continue anyway */ | |
616 | } | |
617 | #endif /* ANDROID */ | |
618 | ||
6fc6879b JM |
619 | if (gid_str) { |
620 | grp = getgrnam(gid_str); | |
621 | if (grp) { | |
622 | gid = grp->gr_gid; | |
623 | gid_set = 1; | |
624 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
625 | " (from group name '%s')", | |
626 | (int) gid, gid_str); | |
627 | } else { | |
628 | /* Group name not found - try to parse this as gid */ | |
629 | gid = strtol(gid_str, &endp, 10); | |
630 | if (*gid_str == '\0' || *endp != '\0') { | |
8e888179 | 631 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " |
6fc6879b JM |
632 | "'%s'", gid_str); |
633 | goto fail; | |
634 | } | |
635 | gid_set = 1; | |
636 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
637 | (int) gid); | |
638 | } | |
639 | } | |
640 | ||
641 | if (gid_set && chown(dir, -1, gid) < 0) { | |
2c6f8cf6 JM |
642 | wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s", |
643 | dir, (int) gid, strerror(errno)); | |
6fc6879b JM |
644 | goto fail; |
645 | } | |
646 | ||
3fd2a226 AAS |
647 | /* Make sure the group can enter and read the directory */ |
648 | if (gid_set && | |
649 | chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) { | |
650 | wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s", | |
651 | strerror(errno)); | |
652 | goto fail; | |
653 | } | |
654 | ||
6fc6879b JM |
655 | if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >= |
656 | sizeof(addr.sun_path)) { | |
657 | wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded"); | |
658 | goto fail; | |
659 | } | |
660 | ||
661 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
662 | if (priv->sock < 0) { | |
2c6f8cf6 | 663 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
6fc6879b JM |
664 | goto fail; |
665 | } | |
666 | ||
667 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 668 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
669 | addr.sun_len = sizeof(addr); |
670 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
671 | addr.sun_family = AF_UNIX; |
672 | fname = wpa_supplicant_ctrl_iface_path(wpa_s); | |
673 | if (fname == NULL) | |
674 | goto fail; | |
675 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
676 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
677 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", | |
678 | strerror(errno)); | |
679 | if (connect(priv->sock, (struct sockaddr *) &addr, | |
680 | sizeof(addr)) < 0) { | |
681 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
682 | " allow connections - assuming it was left" | |
683 | "over from forced program termination"); | |
684 | if (unlink(fname) < 0) { | |
2c6f8cf6 JM |
685 | wpa_printf(MSG_ERROR, |
686 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
687 | fname, strerror(errno)); | |
6fc6879b JM |
688 | goto fail; |
689 | } | |
690 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
691 | sizeof(addr)) < 0) { | |
2c6f8cf6 JM |
692 | wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s", |
693 | strerror(errno)); | |
6fc6879b JM |
694 | goto fail; |
695 | } | |
696 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
697 | "ctrl_iface socket '%s'", fname); | |
698 | } else { | |
699 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
700 | "be in use - cannot override it"); | |
701 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
702 | "not used anymore", fname); | |
703 | os_free(fname); | |
704 | fname = NULL; | |
705 | goto fail; | |
706 | } | |
707 | } | |
708 | ||
709 | if (gid_set && chown(fname, -1, gid) < 0) { | |
2c6f8cf6 JM |
710 | wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s", |
711 | fname, (int) gid, strerror(errno)); | |
6fc6879b JM |
712 | goto fail; |
713 | } | |
714 | ||
715 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { | |
2c6f8cf6 JM |
716 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s", |
717 | fname, strerror(errno)); | |
6fc6879b JM |
718 | goto fail; |
719 | } | |
720 | os_free(fname); | |
721 | ||
b3f3865e DS |
722 | #ifdef ANDROID |
723 | havesock: | |
724 | #endif /* ANDROID */ | |
4fdc8def BG |
725 | |
726 | /* | |
727 | * Make socket non-blocking so that we don't hang forever if | |
728 | * target dies unexpectedly. | |
729 | */ | |
730 | flags = fcntl(priv->sock, F_GETFL); | |
731 | if (flags >= 0) { | |
732 | flags |= O_NONBLOCK; | |
733 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
2c6f8cf6 JM |
734 | wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s", |
735 | strerror(errno)); | |
4fdc8def BG |
736 | /* Not fatal, continue on.*/ |
737 | } | |
738 | } | |
739 | ||
6fc6879b JM |
740 | eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive, |
741 | wpa_s, priv); | |
742 | wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); | |
743 | ||
744 | os_free(buf); | |
89286e91 | 745 | return 0; |
6fc6879b JM |
746 | |
747 | fail: | |
89286e91 | 748 | if (priv->sock >= 0) { |
6fc6879b | 749 | close(priv->sock); |
89286e91 JM |
750 | priv->sock = -1; |
751 | } | |
6fc6879b JM |
752 | if (fname) { |
753 | unlink(fname); | |
754 | os_free(fname); | |
755 | } | |
756 | os_free(buf); | |
89286e91 JM |
757 | return -1; |
758 | } | |
759 | ||
760 | ||
761 | struct ctrl_iface_priv * | |
762 | wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s) | |
763 | { | |
764 | struct ctrl_iface_priv *priv; | |
765 | ||
766 | priv = os_zalloc(sizeof(*priv)); | |
767 | if (priv == NULL) | |
768 | return NULL; | |
769 | dl_list_init(&priv->ctrl_dst); | |
3fdaaa8f | 770 | dl_list_init(&priv->msg_queue); |
89286e91 JM |
771 | priv->wpa_s = wpa_s; |
772 | priv->sock = -1; | |
773 | ||
774 | if (wpa_s->conf->ctrl_interface == NULL) | |
775 | return priv; | |
776 | ||
c9cfa6a9 SD |
777 | #ifdef ANDROID |
778 | if (wpa_s->global->params.ctrl_interface) { | |
779 | int same = 0; | |
780 | ||
781 | if (wpa_s->global->params.ctrl_interface[0] == '/') { | |
782 | if (os_strcmp(wpa_s->global->params.ctrl_interface, | |
783 | wpa_s->conf->ctrl_interface) == 0) | |
784 | same = 1; | |
785 | } else if (os_strncmp(wpa_s->global->params.ctrl_interface, | |
786 | "@android:", 9) == 0 || | |
787 | os_strncmp(wpa_s->global->params.ctrl_interface, | |
788 | "@abstract:", 10) == 0) { | |
789 | char *pos; | |
790 | ||
791 | /* | |
792 | * Currently, Android uses @android:wpa_* as the naming | |
793 | * convention for the global ctrl interface. This logic | |
794 | * needs to be revisited if the above naming convention | |
795 | * is modified. | |
796 | */ | |
797 | pos = os_strchr(wpa_s->global->params.ctrl_interface, | |
798 | '_'); | |
799 | if (pos && | |
800 | os_strcmp(pos + 1, | |
801 | wpa_s->conf->ctrl_interface) == 0) | |
802 | same = 1; | |
803 | } | |
804 | ||
805 | if (same) { | |
806 | /* | |
807 | * The invalid configuration combination might be | |
808 | * possible to hit in an Android OTA upgrade case, so | |
809 | * instead of refusing to start the wpa_supplicant | |
810 | * process, do not open the per-interface ctrl_iface | |
811 | * and continue with the global control interface that | |
812 | * was set from the command line since the Wi-Fi | |
813 | * framework will use it for operations. | |
814 | */ | |
815 | wpa_printf(MSG_ERROR, | |
816 | "global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface", | |
817 | wpa_s->global->params.ctrl_interface, | |
818 | wpa_s->conf->ctrl_interface); | |
819 | return priv; | |
820 | } | |
821 | } | |
822 | #endif /* ANDROID */ | |
823 | ||
89286e91 JM |
824 | if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) { |
825 | os_free(priv); | |
826 | return NULL; | |
827 | } | |
828 | ||
829 | return priv; | |
830 | } | |
831 | ||
832 | ||
833 | static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s, | |
834 | struct ctrl_iface_priv *priv) | |
835 | { | |
836 | int res; | |
837 | ||
838 | if (priv->sock <= 0) | |
839 | return -1; | |
840 | ||
3a7414b6 NM |
841 | /* |
842 | * On Android, the control socket being used may be the socket | |
843 | * that is created when wpa_supplicant is started as a /init.*.rc | |
844 | * service. Such a socket is maintained as a key-value pair in | |
845 | * Android's environment. Closing this control socket would leave us | |
846 | * in a bad state with an invalid socket descriptor. | |
847 | */ | |
848 | if (priv->android_control_socket) | |
849 | return priv->sock; | |
850 | ||
89286e91 JM |
851 | eloop_unregister_read_sock(priv->sock); |
852 | close(priv->sock); | |
853 | priv->sock = -1; | |
854 | res = wpas_ctrl_iface_open_sock(wpa_s, priv); | |
855 | if (res < 0) | |
856 | return -1; | |
857 | return priv->sock; | |
6fc6879b JM |
858 | } |
859 | ||
860 | ||
861 | void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv) | |
862 | { | |
863 | struct wpa_ctrl_dst *dst, *prev; | |
3fdaaa8f JM |
864 | struct ctrl_iface_msg *msg, *prev_msg; |
865 | struct ctrl_iface_global_priv *gpriv; | |
6fc6879b JM |
866 | |
867 | if (priv->sock > -1) { | |
868 | char *fname; | |
6ed626df | 869 | char *buf, *dir = NULL; |
6fc6879b | 870 | eloop_unregister_read_sock(priv->sock); |
09e47a07 | 871 | if (!dl_list_empty(&priv->ctrl_dst)) { |
6fc6879b | 872 | /* |
e0591c3c | 873 | * Wait before closing the control socket if |
6fc6879b JM |
874 | * there are any attached monitors in order to allow |
875 | * them to receive any pending messages. | |
876 | */ | |
877 | wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached " | |
878 | "monitors to receive messages"); | |
e0591c3c | 879 | os_sleep(0, 100000); |
6fc6879b JM |
880 | } |
881 | close(priv->sock); | |
882 | priv->sock = -1; | |
883 | fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s); | |
884 | if (fname) { | |
885 | unlink(fname); | |
886 | os_free(fname); | |
887 | } | |
888 | ||
ea61aa1d JM |
889 | if (priv->wpa_s->conf->ctrl_interface == NULL) |
890 | goto free_dst; | |
6fc6879b JM |
891 | buf = os_strdup(priv->wpa_s->conf->ctrl_interface); |
892 | if (buf == NULL) | |
893 | goto free_dst; | |
894 | if (os_strncmp(buf, "DIR=", 4) == 0) { | |
6ed626df | 895 | char *gid_str; |
6fc6879b JM |
896 | dir = buf + 4; |
897 | gid_str = os_strstr(dir, " GROUP="); | |
6ed626df | 898 | if (gid_str) |
6fc6879b | 899 | *gid_str = '\0'; |
6fc6879b JM |
900 | } else |
901 | dir = buf; | |
902 | ||
903 | if (rmdir(dir) < 0) { | |
904 | if (errno == ENOTEMPTY) { | |
905 | wpa_printf(MSG_DEBUG, "Control interface " | |
906 | "directory not empty - leaving it " | |
907 | "behind"); | |
908 | } else { | |
2c6f8cf6 JM |
909 | wpa_printf(MSG_ERROR, |
910 | "rmdir[ctrl_interface=%s]: %s", | |
911 | dir, strerror(errno)); | |
6fc6879b JM |
912 | } |
913 | } | |
914 | os_free(buf); | |
915 | } | |
916 | ||
917 | free_dst: | |
09e47a07 | 918 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
12ea4cff JM |
919 | list) { |
920 | dl_list_del(&dst->list); | |
09e47a07 | 921 | os_free(dst); |
12ea4cff | 922 | } |
3fdaaa8f JM |
923 | dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue, |
924 | struct ctrl_iface_msg, list) { | |
925 | dl_list_del(&msg->list); | |
926 | os_free(msg); | |
927 | } | |
928 | gpriv = priv->wpa_s->global->ctrl_iface; | |
929 | if (gpriv) { | |
930 | dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue, | |
931 | struct ctrl_iface_msg, list) { | |
932 | if (msg->wpa_s == priv->wpa_s) { | |
933 | dl_list_del(&msg->list); | |
934 | os_free(msg); | |
935 | } | |
936 | } | |
937 | } | |
938 | eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL); | |
6fc6879b JM |
939 | os_free(priv); |
940 | } | |
941 | ||
942 | ||
943 | /** | |
944 | * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors | |
214e428b JM |
945 | * @ifname: Interface name for global control socket or %NULL |
946 | * @sock: Local socket fd | |
947 | * @ctrl_dst: List of attached listeners | |
6fc6879b JM |
948 | * @level: Priority level of the message |
949 | * @buf: Message data | |
950 | * @len: Message length | |
951 | * | |
952 | * Send a packet to all monitor programs attached to the control interface. | |
953 | */ | |
89286e91 JM |
954 | static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s, |
955 | const char *ifname, int sock, | |
214e428b | 956 | struct dl_list *ctrl_dst, |
6fc6879b | 957 | int level, const char *buf, |
89286e91 JM |
958 | size_t len, |
959 | struct ctrl_iface_priv *priv, | |
960 | struct ctrl_iface_global_priv *gp) | |
6fc6879b JM |
961 | { |
962 | struct wpa_ctrl_dst *dst, *next; | |
963 | char levelstr[10]; | |
964 | int idx, res; | |
965 | struct msghdr msg; | |
214e428b | 966 | struct iovec io[5]; |
6fc6879b | 967 | |
214e428b | 968 | if (sock < 0 || dl_list_empty(ctrl_dst)) |
6fc6879b JM |
969 | return; |
970 | ||
971 | res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
d85e1fc8 | 972 | if (os_snprintf_error(sizeof(levelstr), res)) |
6fc6879b | 973 | return; |
214e428b JM |
974 | idx = 0; |
975 | if (ifname) { | |
976 | io[idx].iov_base = "IFNAME="; | |
977 | io[idx].iov_len = 7; | |
978 | idx++; | |
979 | io[idx].iov_base = (char *) ifname; | |
980 | io[idx].iov_len = os_strlen(ifname); | |
981 | idx++; | |
982 | io[idx].iov_base = " "; | |
983 | io[idx].iov_len = 1; | |
984 | idx++; | |
985 | } | |
986 | io[idx].iov_base = levelstr; | |
987 | io[idx].iov_len = os_strlen(levelstr); | |
988 | idx++; | |
989 | io[idx].iov_base = (char *) buf; | |
990 | io[idx].iov_len = len; | |
991 | idx++; | |
6fc6879b JM |
992 | os_memset(&msg, 0, sizeof(msg)); |
993 | msg.msg_iov = io; | |
214e428b | 994 | msg.msg_iovlen = idx; |
6fc6879b | 995 | |
214e428b | 996 | dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { |
89286e91 | 997 | int _errno; |
c9b55597 | 998 | char addr_txt[200]; |
89286e91 | 999 | |
89286e91 JM |
1000 | if (level < dst->debug_level) |
1001 | continue; | |
1002 | ||
c9b55597 JM |
1003 | printf_encode(addr_txt, sizeof(addr_txt), |
1004 | (u8 *) dst->addr.sun_path, dst->addrlen - | |
1005 | offsetof(struct sockaddr_un, sun_path)); | |
89286e91 JM |
1006 | msg.msg_name = (void *) &dst->addr; |
1007 | msg.msg_namelen = dst->addrlen; | |
2e95cfc1 | 1008 | wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len); |
89286e91 | 1009 | if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) { |
43fa110b JM |
1010 | wpa_printf(MSG_MSGDUMP, |
1011 | "CTRL_IFACE monitor sent successfully to %s", | |
c9b55597 | 1012 | addr_txt); |
89286e91 | 1013 | dst->errors = 0; |
89286e91 JM |
1014 | continue; |
1015 | } | |
1016 | ||
1017 | _errno = errno; | |
c9b55597 JM |
1018 | wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s", |
1019 | addr_txt, errno, strerror(errno)); | |
89286e91 JM |
1020 | dst->errors++; |
1021 | ||
1022 | if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) { | |
c9b55597 JM |
1023 | wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages", |
1024 | addr_txt); | |
89286e91 JM |
1025 | wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr, |
1026 | dst->addrlen); | |
1027 | } | |
1028 | ||
1029 | if (_errno == ENOBUFS || _errno == EAGAIN) { | |
1030 | /* | |
1031 | * The socket send buffer could be full. This may happen | |
1032 | * if client programs are not receiving their pending | |
1033 | * messages. Close and reopen the socket as a workaround | |
1034 | * to avoid getting stuck being unable to send any new | |
1035 | * responses. | |
1036 | */ | |
1037 | if (priv) | |
1038 | sock = wpas_ctrl_iface_reinit(wpa_s, priv); | |
1039 | else if (gp) | |
1040 | sock = wpas_ctrl_iface_global_reinit( | |
1041 | wpa_s->global, gp); | |
1042 | else | |
1043 | break; | |
1044 | if (sock < 0) { | |
1045 | wpa_dbg(wpa_s, MSG_DEBUG, | |
1046 | "Failed to reinitialize ctrl_iface socket"); | |
29179b88 | 1047 | break; |
89286e91 JM |
1048 | } |
1049 | } | |
6fc6879b JM |
1050 | } |
1051 | } | |
1052 | ||
1053 | ||
1054 | void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) | |
1055 | { | |
1056 | char buf[256]; | |
1057 | int res; | |
1058 | struct sockaddr_un from; | |
1059 | socklen_t fromlen = sizeof(from); | |
1060 | ||
1061 | for (;;) { | |
1062 | wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to " | |
1063 | "attach", priv->wpa_s->ifname); | |
1064 | eloop_wait_for_read_sock(priv->sock); | |
1065 | ||
1066 | res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0, | |
1067 | (struct sockaddr *) &from, &fromlen); | |
1068 | if (res < 0) { | |
2c6f8cf6 JM |
1069 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
1070 | strerror(errno)); | |
6fc6879b JM |
1071 | continue; |
1072 | } | |
1073 | buf[res] = '\0'; | |
1074 | ||
1075 | if (os_strcmp(buf, "ATTACH") == 0) { | |
1076 | /* handle ATTACH signal of first monitor interface */ | |
214e428b | 1077 | if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, |
0efcad2c JM |
1078 | &from, fromlen, |
1079 | 0)) { | |
79986bf6 JM |
1080 | if (sendto(priv->sock, "OK\n", 3, 0, |
1081 | (struct sockaddr *) &from, fromlen) < | |
1082 | 0) { | |
1083 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
1084 | strerror(errno)); | |
1085 | } | |
6fc6879b JM |
1086 | /* OK to continue */ |
1087 | return; | |
1088 | } else { | |
79986bf6 JM |
1089 | if (sendto(priv->sock, "FAIL\n", 5, 0, |
1090 | (struct sockaddr *) &from, fromlen) < | |
1091 | 0) { | |
1092 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
1093 | strerror(errno)); | |
1094 | } | |
6fc6879b JM |
1095 | } |
1096 | } else { | |
1097 | /* return FAIL for all other signals */ | |
79986bf6 JM |
1098 | if (sendto(priv->sock, "FAIL\n", 5, 0, |
1099 | (struct sockaddr *) &from, fromlen) < 0) { | |
1100 | wpa_printf(MSG_DEBUG, | |
1101 | "ctrl_iface sendto failed: %s", | |
1102 | strerror(errno)); | |
1103 | } | |
6fc6879b JM |
1104 | } |
1105 | } | |
1106 | } | |
1107 | ||
1108 | ||
1109 | /* Global ctrl_iface */ | |
1110 | ||
6fc6879b JM |
1111 | static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx, |
1112 | void *sock_ctx) | |
1113 | { | |
1114 | struct wpa_global *global = eloop_ctx; | |
214e428b | 1115 | struct ctrl_iface_global_priv *priv = sock_ctx; |
8615bdfa | 1116 | char buf[4096]; |
6fc6879b JM |
1117 | int res; |
1118 | struct sockaddr_un from; | |
1119 | socklen_t fromlen = sizeof(from); | |
742e715b | 1120 | char *reply = NULL, *reply_buf = NULL; |
6fc6879b JM |
1121 | size_t reply_len; |
1122 | ||
1123 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
1124 | (struct sockaddr *) &from, &fromlen); | |
1125 | if (res < 0) { | |
2c6f8cf6 JM |
1126 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
1127 | strerror(errno)); | |
6fc6879b JM |
1128 | return; |
1129 | } | |
1130 | buf[res] = '\0'; | |
1131 | ||
214e428b JM |
1132 | if (os_strcmp(buf, "ATTACH") == 0) { |
1133 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, | |
0efcad2c | 1134 | fromlen, 1)) |
214e428b JM |
1135 | reply_len = 1; |
1136 | else | |
1137 | reply_len = 2; | |
1138 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
1139 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, | |
1140 | fromlen)) | |
1141 | reply_len = 1; | |
1142 | else | |
1143 | reply_len = 2; | |
1144 | } else { | |
742e715b JM |
1145 | reply_buf = wpa_supplicant_global_ctrl_iface_process( |
1146 | global, buf, &reply_len); | |
1147 | reply = reply_buf; | |
14fd0331 JM |
1148 | |
1149 | /* | |
1150 | * There could be some password/key material in the command, so | |
1151 | * clear the buffer explicitly now that it is not needed | |
1152 | * anymore. | |
1153 | */ | |
1154 | os_memset(buf, 0, res); | |
742e715b JM |
1155 | } |
1156 | ||
1157 | if (!reply && reply_len == 1) { | |
1158 | reply = "FAIL\n"; | |
1159 | reply_len = 5; | |
1160 | } else if (!reply && reply_len == 2) { | |
1161 | reply = "OK\n"; | |
1162 | reply_len = 3; | |
214e428b | 1163 | } |
6fc6879b JM |
1164 | |
1165 | if (reply) { | |
2e95cfc1 JM |
1166 | wpas_ctrl_sock_debug("global_ctrl_sock-sendto", |
1167 | sock, reply, reply_len); | |
79986bf6 JM |
1168 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
1169 | fromlen) < 0) { | |
1170 | wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s", | |
1171 | strerror(errno)); | |
1172 | } | |
6fc6879b | 1173 | } |
742e715b | 1174 | os_free(reply_buf); |
6fc6879b JM |
1175 | } |
1176 | ||
1177 | ||
89286e91 JM |
1178 | static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global, |
1179 | struct ctrl_iface_global_priv *priv) | |
6fc6879b | 1180 | { |
6fc6879b | 1181 | struct sockaddr_un addr; |
d2a9e2c7 | 1182 | const char *ctrl = global->params.ctrl_interface; |
8d6e0350 | 1183 | int flags; |
6fc6879b | 1184 | |
d2a9e2c7 JM |
1185 | wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl); |
1186 | ||
b3f3865e | 1187 | #ifdef ANDROID |
d2a9e2c7 JM |
1188 | if (os_strncmp(ctrl, "@android:", 9) == 0) { |
1189 | priv->sock = android_get_control_socket(ctrl + 9); | |
1190 | if (priv->sock < 0) { | |
1191 | wpa_printf(MSG_ERROR, "Failed to open Android control " | |
1192 | "socket '%s'", ctrl + 9); | |
1193 | goto fail; | |
1194 | } | |
1195 | wpa_printf(MSG_DEBUG, "Using Android control socket '%s'", | |
1196 | ctrl + 9); | |
3a7414b6 | 1197 | priv->android_control_socket = 1; |
b3f3865e | 1198 | goto havesock; |
d2a9e2c7 | 1199 | } |
b3f3865e | 1200 | |
d2a9e2c7 JM |
1201 | if (os_strncmp(ctrl, "@abstract:", 10) != 0) { |
1202 | /* | |
1203 | * Backwards compatibility - try to open an Android control | |
1204 | * socket and if that fails, assume this was a UNIX domain | |
1205 | * socket instead. | |
1206 | */ | |
1207 | priv->sock = android_get_control_socket(ctrl); | |
1208 | if (priv->sock >= 0) { | |
1209 | wpa_printf(MSG_DEBUG, | |
1210 | "Using Android control socket '%s'", | |
1211 | ctrl); | |
3a7414b6 | 1212 | priv->android_control_socket = 1; |
d2a9e2c7 JM |
1213 | goto havesock; |
1214 | } | |
1215 | } | |
1216 | #endif /* ANDROID */ | |
6fc6879b JM |
1217 | |
1218 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
1219 | if (priv->sock < 0) { | |
2c6f8cf6 | 1220 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
6fc6879b JM |
1221 | goto fail; |
1222 | } | |
1223 | ||
1224 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 1225 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
1226 | addr.sun_len = sizeof(addr); |
1227 | #endif /* __FreeBSD__ */ | |
6fc6879b | 1228 | addr.sun_family = AF_UNIX; |
d2a9e2c7 JM |
1229 | |
1230 | if (os_strncmp(ctrl, "@abstract:", 10) == 0) { | |
1231 | addr.sun_path[0] = '\0'; | |
1232 | os_strlcpy(addr.sun_path + 1, ctrl + 10, | |
1233 | sizeof(addr.sun_path) - 1); | |
1234 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < | |
1235 | 0) { | |
1236 | wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: " | |
2c6f8cf6 JM |
1237 | "bind(PF_UNIX;%s) failed: %s", |
1238 | ctrl, strerror(errno)); | |
d2a9e2c7 JM |
1239 | goto fail; |
1240 | } | |
1241 | wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'", | |
1242 | ctrl + 10); | |
1243 | goto havesock; | |
1244 | } | |
1245 | ||
1246 | os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path)); | |
6fc6879b | 1247 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
2c6f8cf6 JM |
1248 | wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s", |
1249 | ctrl, strerror(errno)); | |
6fc6879b JM |
1250 | if (connect(priv->sock, (struct sockaddr *) &addr, |
1251 | sizeof(addr)) < 0) { | |
1252 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
1253 | " allow connections - assuming it was left" | |
1254 | "over from forced program termination"); | |
d2a9e2c7 | 1255 | if (unlink(ctrl) < 0) { |
2c6f8cf6 JM |
1256 | wpa_printf(MSG_ERROR, |
1257 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
1258 | ctrl, strerror(errno)); | |
6fc6879b JM |
1259 | goto fail; |
1260 | } | |
1261 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
1262 | sizeof(addr)) < 0) { | |
2c6f8cf6 JM |
1263 | wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s", |
1264 | ctrl, strerror(errno)); | |
6fc6879b JM |
1265 | goto fail; |
1266 | } | |
1267 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
1268 | "ctrl_iface socket '%s'", | |
d2a9e2c7 | 1269 | ctrl); |
6fc6879b JM |
1270 | } else { |
1271 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
1272 | "be in use - cannot override it"); | |
1273 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
1274 | "not used anymore", | |
d2a9e2c7 | 1275 | ctrl); |
6fc6879b JM |
1276 | goto fail; |
1277 | } | |
1278 | } | |
1279 | ||
d2a9e2c7 JM |
1280 | wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl); |
1281 | ||
29257565 JM |
1282 | if (global->params.ctrl_interface_group) { |
1283 | char *gid_str = global->params.ctrl_interface_group; | |
1284 | gid_t gid = 0; | |
1285 | struct group *grp; | |
1286 | char *endp; | |
1287 | ||
1288 | grp = getgrnam(gid_str); | |
1289 | if (grp) { | |
1290 | gid = grp->gr_gid; | |
1291 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
1292 | " (from group name '%s')", | |
1293 | (int) gid, gid_str); | |
1294 | } else { | |
1295 | /* Group name not found - try to parse this as gid */ | |
1296 | gid = strtol(gid_str, &endp, 10); | |
1297 | if (*gid_str == '\0' || *endp != '\0') { | |
1298 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " | |
1299 | "'%s'", gid_str); | |
1300 | goto fail; | |
1301 | } | |
1302 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
1303 | (int) gid); | |
1304 | } | |
d2a9e2c7 | 1305 | if (chown(ctrl, -1, gid) < 0) { |
2c6f8cf6 JM |
1306 | wpa_printf(MSG_ERROR, |
1307 | "chown[global_ctrl_interface=%s,gid=%d]: %s", | |
1308 | ctrl, (int) gid, strerror(errno)); | |
29257565 JM |
1309 | goto fail; |
1310 | } | |
1311 | ||
d2a9e2c7 | 1312 | if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) { |
2c6f8cf6 JM |
1313 | wpa_printf(MSG_ERROR, |
1314 | "chmod[global_ctrl_interface=%s]: %s", | |
1315 | ctrl, strerror(errno)); | |
29257565 JM |
1316 | goto fail; |
1317 | } | |
d2a9e2c7 | 1318 | } else { |
f62415df JM |
1319 | if (chmod(ctrl, S_IRWXU) < 0) { |
1320 | wpa_printf(MSG_DEBUG, | |
1321 | "chmod[global_ctrl_interface=%s](S_IRWXU): %s", | |
1322 | ctrl, strerror(errno)); | |
1323 | /* continue anyway since group change was not required | |
1324 | */ | |
1325 | } | |
29257565 JM |
1326 | } |
1327 | ||
b3f3865e | 1328 | havesock: |
8d6e0350 JM |
1329 | |
1330 | /* | |
1331 | * Make socket non-blocking so that we don't hang forever if | |
1332 | * target dies unexpectedly. | |
1333 | */ | |
1334 | flags = fcntl(priv->sock, F_GETFL); | |
1335 | if (flags >= 0) { | |
1336 | flags |= O_NONBLOCK; | |
1337 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
2c6f8cf6 JM |
1338 | wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s", |
1339 | strerror(errno)); | |
8d6e0350 JM |
1340 | /* Not fatal, continue on.*/ |
1341 | } | |
1342 | } | |
1343 | ||
6fc6879b JM |
1344 | eloop_register_read_sock(priv->sock, |
1345 | wpa_supplicant_global_ctrl_iface_receive, | |
214e428b | 1346 | global, priv); |
6fc6879b | 1347 | |
89286e91 | 1348 | return 0; |
6fc6879b JM |
1349 | |
1350 | fail: | |
89286e91 | 1351 | if (priv->sock >= 0) { |
6fc6879b | 1352 | close(priv->sock); |
89286e91 JM |
1353 | priv->sock = -1; |
1354 | } | |
1355 | return -1; | |
1356 | } | |
1357 | ||
1358 | ||
1359 | struct ctrl_iface_global_priv * | |
1360 | wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global) | |
1361 | { | |
1362 | struct ctrl_iface_global_priv *priv; | |
1363 | ||
1364 | priv = os_zalloc(sizeof(*priv)); | |
1365 | if (priv == NULL) | |
1366 | return NULL; | |
1367 | dl_list_init(&priv->ctrl_dst); | |
3fdaaa8f | 1368 | dl_list_init(&priv->msg_queue); |
89286e91 JM |
1369 | priv->global = global; |
1370 | priv->sock = -1; | |
1371 | ||
1372 | if (global->params.ctrl_interface == NULL) | |
1373 | return priv; | |
1374 | ||
1375 | if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) { | |
1376 | os_free(priv); | |
1377 | return NULL; | |
1378 | } | |
1379 | ||
76fe79ef JM |
1380 | wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); |
1381 | ||
89286e91 JM |
1382 | return priv; |
1383 | } | |
1384 | ||
1385 | ||
1386 | static int wpas_ctrl_iface_global_reinit(struct wpa_global *global, | |
1387 | struct ctrl_iface_global_priv *priv) | |
1388 | { | |
1389 | int res; | |
1390 | ||
1391 | if (priv->sock <= 0) | |
1392 | return -1; | |
1393 | ||
3a7414b6 NM |
1394 | /* |
1395 | * On Android, the control socket being used may be the socket | |
1396 | * that is created when wpa_supplicant is started as a /init.*.rc | |
1397 | * service. Such a socket is maintained as a key-value pair in | |
1398 | * Android's environment. Closing this control socket would leave us | |
1399 | * in a bad state with an invalid socket descriptor. | |
1400 | */ | |
1401 | if (priv->android_control_socket) | |
1402 | return priv->sock; | |
1403 | ||
89286e91 JM |
1404 | eloop_unregister_read_sock(priv->sock); |
1405 | close(priv->sock); | |
1406 | priv->sock = -1; | |
1407 | res = wpas_global_ctrl_iface_open_sock(global, priv); | |
1408 | if (res < 0) | |
1409 | return -1; | |
1410 | return priv->sock; | |
6fc6879b JM |
1411 | } |
1412 | ||
1413 | ||
1414 | void | |
1415 | wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv) | |
1416 | { | |
214e428b | 1417 | struct wpa_ctrl_dst *dst, *prev; |
3fdaaa8f | 1418 | struct ctrl_iface_msg *msg, *prev_msg; |
214e428b | 1419 | |
6fc6879b JM |
1420 | if (priv->sock >= 0) { |
1421 | eloop_unregister_read_sock(priv->sock); | |
1422 | close(priv->sock); | |
1423 | } | |
1424 | if (priv->global->params.ctrl_interface) | |
1425 | unlink(priv->global->params.ctrl_interface); | |
214e428b | 1426 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
12ea4cff JM |
1427 | list) { |
1428 | dl_list_del(&dst->list); | |
214e428b | 1429 | os_free(dst); |
12ea4cff | 1430 | } |
3fdaaa8f JM |
1431 | dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue, |
1432 | struct ctrl_iface_msg, list) { | |
1433 | dl_list_del(&msg->list); | |
1434 | os_free(msg); | |
1435 | } | |
6fc6879b JM |
1436 | os_free(priv); |
1437 | } |